When it is required to unpack a tuple of list, the 'reduce' method can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A tuple of list contains multiple lists, which are enclosed in '(' and ')'.
The 'reduce' method is used to apply a specific method (that is passed as an argument to it) to all the values in the iterable. This method is present in the 'functools' module.
Below is a demonstration for the same −
from functools import reduce import operator def unpack_tuple(my_tup): return (reduce(operator.add, my_tup)) my_tuple = (['h', 'jane'], ['m', 'may']) print("The tuple of list is") print(my_tuple) print("After unpacking, it is") print(unpack_tuple(my_tuple))
The tuple of list is (['h', 'jane'], ['m', 'may']) After unpacking, it is ['h', 'jane', 'm', 'may']