When it is required to remove the duplicates present in tuple of list, as well as preserving the order, a list comprehension and the 'set' method can be used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
Python comes with a datatype known as 'set'. This 'set' contains elements that are unique only. The set is useful in performing operations such as intersection, difference, union and symmetric difference.
Below is a demonstration of the same −
my_tuple_1 = ([1, 21, 34] , [11, 0, 98], [45, 67, 56]) print("The tuple of list is : ") print(my_tuple_1) temp_val = set() my_result = [elem for elem in my_tuple_1 if not(tuple(elem) in temp_val or temp_val.add(tuple(elem)))] print("The unique tuple of list is : ") print(my_result)
The tuple of list is : ([1, 21, 34], [11, 0, 98], [45, 67, 56]) The unique tuple of list is : [[1, 21, 34], [11, 0, 98], [45, 67, 56]]