Some Basic Python Tuple Programs-2

Python Program to check if a tuple and a list are identical

tuple = (9, 8, 7, 6, 5)
list = [9, 8, 7, 6, 5]

#using loop
flag1 = True
for i in range(0, len(tuple)):
    if tuple[i] != list[i]:
       flag1 = False
if flag1:
    print(f"{tuple} and {list} are identical") 
    print(f"{tuple} and {list} are not identical") 
#Output: (9, 8, 7, 6, 5) and [9, 8, 7, 6, 5] are identical

#using all() + zip()
flag2 = all([ele1 == ele2 for ele1,ele2 in zip(tuple, list)])
if flag2: 
    print(f"{tuple} and {list} are identical") 
    print(f"{tuple} and {list} are not identical") 
#Output: (9, 8, 7, 6, 5) and [9, 8, 7, 6, 5] are identical
  • The len() built-in function returns the number of elements present in a container.
  • Python treats everything as an object and objects are always true in python. However, null values, None, 0, empty data types are observed as false in python.
  • The format() built-in string method returns a formatted representation of the string controlled by the format specifier. The prefix f before a string tells Python that the string is a format string. The replacement fields are expressions enclosed in curly brackets in a format string. They are evaluated at run time and then formatted.
  • The all() built-in method returns True If all the elements in the iterable passed are true else it returns False.
  • 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. Tuple unpacking allows extracting tuple elements automatically by assigning each value on the right-hand side to its respective variable on the left side.

Python Program to add multiple tuples

tup1 = (1, 2, 3, 4)
tup2 = (5, 6, 7, 8)
tup3 = (9, 10, 11, 12)

sum_tup = tuple(map(sum,zip(tup1, tup2, tup3)))
#Output: (15, 18, 21, 24)
  • The tuple() built-in method takes an iterable and creates a new tuple of its elements.
  • The built-in map() function applies a given function to each element of an iterable and returns a list of the results.
  • The built-in sum() function adds the elements of an iterable and returns the sum.

Python Program to get the sum of tuples having the same first value

from collections import defaultdict 
list_tup = [(1, 8), (2, 1), (2,28), (1, 20), (3, 15), (1, 15)]
sum = defaultdict(int)
for key, value in list_tup:
    sum[key] += value
#Output: [(1, 43), (2, 29), (3, 15)]
  • Defaultdict is a sub-class of the dict class that works exactly like a normal dictionary, but it is initialized with a function that takes no arguments and returns the default value to a key that does not exist. If the key is not present in the Default Dictionary while accessing it, it will create the key and automatically assigns the default value to the key. The int function defaults a key’s value to 0. Read more: What is Ordered Dictionary and Default Dictionary in Python?
  • The list() built-in method takes an iterables and creates a new list of its elements.

Python Program to subtract two tuples

tup1 = (9, 12, 17, 5)
tup2 = (15, 19, 1, 7)
sub = tuple(map(lambda ele1, ele2: ele1 - ele2, tup1, tup2)) 
#Output: (-6, -14, 16, 5)
  • The lambda function is a function that is defined without a name. It can have any number of arguments but only one expression as it is a small function having a single line of code. The expression is evaluated and returned.

Python Program to find the summation of tuples in a list

list_tup = [(1, 2, 3), (4,), (5, 6), (7, 8, 9), (0, 10)]
summation = sum(map(sum, list_tup))
#Ouput: 55
  • Creating a tuple containing one element is a bit tricky. The single element should have a comma at the end because it is the comma that defines a tuple not parenthesis.

Related Posts-

Leave a Comment

%d bloggers like this: