Some Basic Python Tuple Programs-3

Python program to find the minimum and maximum value in a list of tuples

tuple_list = [(6, 5), (3, 8), (1, 6), (4, 7), (3, 1)]
ele1 = tuple(map(max, zip(*tuple_list)))
ele2 = tuple(map(min, zip(*tuple_list)))
print(ele1, el2)
#Output: (6, 8) (1, 1)
  • The built-in max() function returns the largest element in an iterable.
  • The built-in min() function returns the smallest element in an iterable.
  • 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 ‘*’ operator unpacks every element of an iterable to other variables dynamically.
  • The built-in map() function applies a given function to each element of an iterable and returns a list of the results.
  • The tuple() built-in method takes an iterable and creates a new tuple of its elements.

Python program to get the minimum difference in Tuple pair

tuple_list = [(1, 7), (6, 9), (3, 10), (5, 3), (8, 4), (2, 7)]

#using list comprehension
diff_list = [abs(ele2 - ele1) for ele1, ele2 in tuple_list]
diff1 = min(diff_list)
#Output: 2
  • List comprehension is a compact method that defines a list and its contents at the same time. Its basic syntax consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element. Read more: List Comprehension in Python – Explained with Examples
  • The built-in abs() function returns the absolute value of a number.

Python program to get the union of Tuples

tuple1 = (7, 6, 8)
tuple2 = (4, 2, 6, 4, 8)

#using + operator
tuple3 = tuple(set(tuple1 + tuple2))
#Output: (2, 4, 6, 7, 8)

#using union()
tuple4 = tuple(set(tuple1).union(set(tuple2)))
#Output: (2, 4, 6, 7, 8)
  • The ‘+’ operator joins multiple tuples together.
  • The set() built-in method creates a set of the iterable passed. A set in python represents a mathematical notion of a set and performs set operations like union, intersection, difference, etc. It is an unordered collection of immutable and unique elements in python. Read more: Set Data Type in Python

Python program to perform AND operation between Tuples

tuple1 = (6, 2, 3)
tuple2 = (1, 8, 2)
and_tuple = tuple(map(lambda ele1, ele2: ele1 & ele2, tuple1, tuple2))
#Output: (0, 0, 2)
  • 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.
  • The & operator performs bitwise and operation between elements of the tuples having similar indexes. It returns 1 if both the bits are 1, otherwise, it returns 0.

Python program to perform XOR operation on tuples

tuple1 = (9, 6, 1)
tuple2 = (4, 6, 8)
XOR_tuple = tuple(ele1 ^ ele2 for ele1, ele2 in zip(tuple1, tuple2))
#Output: (13, 0, 9)
  • The ^ operator performs the bitwise XOR operation between elements of the tuples having similar indexes. It returns 1 if one of the bits is 0 and the other bit is 1. If both the bits are 0 or 1, then it returns 0.

Python program to convert string tuples to list tuples

list_tup1 = ["('BetaPython:', 2)", "('Python', 10)", "('for', 9)", "('U', 8)"]

#using list comprehension
list_tup2 = [eval(ele) for ele in list_tup1]
#Output: [('BetaPython:', 2), ('Python', 10), ('for', 9), ('U', 8)]

#using map()
list_tup3 = list(map(eval, list_tup1))
#Output: [('BetaPython:', 2), ('Python', 10), ('for', 9), ('U', 8)]
  • The built-in eval() function evaluates the python string expression passed as an argument runs within the program and returns the result as an integer.

Related Posts-

Leave a Comment

%d bloggers like this: