Some Basic Python Tuple Programs-1


Tuple

A tuple is an immutable and sequential collection of data that can be indexed and sliced. Python tuple can contain homogeneous as well as heterogeneous values of data types such as integers, floats, strings, lists, and dictionaries.

Python Program to concatenate multiple tuples

tup1 = (7, 1, 9)
tup2 = (5, 2, 3, 4, 1)

#using + operator 
tup3 = tup1 + tup2 + (7, 8) #tuples are immutable
print(tup3)
#Output: (7, 1, 9, 5, 2, 3, 4, 1, 7, 8)

#using sum() method 
tup4 = sum((tup1, tup2),())
print(tup4)
#Output: (7, 1, 9, 5, 2, 3, 4, 1)
  • The ‘+’ operator joins multiple tuples together.
  • The built-in sum() method adds multiple tuples and returns a newly formed tuple.
  • No original tuples are changed in these methods.

Python Program to modify a tuple

Once a tuple is created, its elements are unchangeable. However, overloading a tuple with existing tuple and change in the tuple is possible. There’s also another way to modify a tuple, converting the tuple into a list, modifying the list, and converting the list back into a tuple.

tup = (25, 11, 2, 10, 15, 7, 23) #tuples are immutable

#adding elements in tuple
tup = tup + (16, 3)
print(tup)
#Output: (25, 11, 2, 10, 15, 7, 23, 16, 3)

#adding elements at a specific index
tup = tup[:2] + (21, 24, 29) + tup[2:] #adding at third position
print(tup) 
#Output: (25, 11, 21, 24, 29, 2, 10, 15, 7, 23, 16, 3)

#removing elements from a tuple
tup = tup[:6] + tup[7:] #removing element from 6th position
print(tup) 
#Output: (25, 11, 21, 24, 29, 2, 15, 7, 23, 16, 3)

#converting tuples into list, modifying it and converting it back
list = list(tup)
list.append(17)
list.extend((26, 4, 8))
list.insert(1,11)
list.remove(29)
list.pop()
tup = tuple(list)
print(tup)
#Output: (25, 11, 11, 21, 24, 2, 15, 7, 23, 16, 3, 17, 26, 4)

Python Program to Chunk Tuples

tup = (6, 14, 17, 26 , 9, 15, 23, 21, 5, 29)
noc = 2 #number of elemenys in one chunk

#using list comprehension
chunks1 = [tup[i : i + noc] for i in range(0, len(tup), noc)]
print(chunks1)
#Output: [(6, 14), (17, 26), (9, 15), (23, 21), (5, 29)] 

#using zip() + iter() methods
list = [iter(tup)] * noc
chunks2 = list(zip(*temp))
print(chunks2) 
#Output: [(6, 14), (17, 26), (9, 15), (23, 21), (5, 29)]
  • List comprehension is a compact method that defines a list and its contents at the same time according to some suitable valid expression.
  • The zip() built-in method returns an iterator of tuples, grouping the similar index of multiple sequences so that they can be used as a single entity.
  • The iter() function creates an iterator object. An iterable is an object which can return its element one by one.

Python Program to Flatten a Tuple of List to a Tuple

Flatten means the entire data object is mapped in one dimension.

tup = ([3, 10, 16], [5], [22, 13, 7, 26])
tup = tuple(sum(tup, [])) 
print(tup)
#Output: (3, 10, 16, 5, 22, 13, 7, 26)

Python Program to unique elements in a nested tuple

nested_tup = [(3, 8, 18, 5), (18, 19, 6, 12, 9), (1, 7, 18)]

#using list comprehension
unique = []
for sub_tuple in nested_tup:
    list = [ele for ele in sub_tuple if not ele in unique]  
    unique.extend(list)
print(unique)
#Output: [3, 8, 18, 5, 19, 6, 1, 9, 7]         

#flattening the nested tuple and using set()
tup = tuple(sum(nested_tup, ())) 
unique = tuple(set(tup))
print(unique)
#Output: (1, 3, 5, 6, 7, 8, 9, 18, 19)
  • The extend() built-in list method adds each element of the iterable passed as an argument to the end of the list. The length of the list increases by the number of elements in the iterable.
  • The set() built-in method creates a set of the iterable passed. A set is an unordered collection of immutable and unique elements in python.

Python Program to Filter Tuples According to List Element Presence

tup = [(2, 13, 5, 9), (1, 5, 7, 4), (13, 8, 20), (9, 12, 20, 11)]
list = [7, 12, 20, 1]
filtered_tup = [sub_tup for sub_tup in tup if (set(list) & set(sub_tup))]
print(filtered_tup)
#Output: [(1, 5, 7, 4), (13, 8, 20), (9, 12, 20, 11)]
  • In Python, list comprehension is mainly used to filter out unwanted elements by testing a suitable valid condition.
  • The and operation performs an intersection between the sets and returns a resulting set that contains only elements that are present in all of the specified sets. Python treats everything as true. However, null values, None, 0, empty data types are observed as false in python.

Leave a Comment

%d bloggers like this: