![]() |
VOOZH | about |
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list.
Examples:
Input : [(1, 2), (5, 7), (3, 6), (1, 2)]
Output : [(1, 2), (5, 7), (3, 6)]
Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')]
Output : [('a', 'z'), ('a', 'x'), ('z', 'x')]
Method #1 : List comprehension This is a naive approach to use list comprehension. Here, we use two for loops and set data structure to cancel out all the duplicates.
[(1, 2), (5, 7), (3, 6)]
Time complexity: O(n*logn), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #2 : List comprehension (Efficient approach) This method is efficient as compared to the above method, here we use a single for loop within list comprehension and then convert it to set to remove duplicates and then again convert it to list.
[(1, 2), (5, 7), (3, 6)]
Time complexity: O(n) where n is the number of tuples in the list.
Auxiliary space: O(n).
Method #3 : Python enumerate() method
[[1, 2], [5, 7], [3, 6]]
Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n^2).
Method #4: Using a dictionary
This method involves creating a dictionary where the keys are the tuples, and the values are a boolean indicating whether the tuple has been encountered before. We can then iterate through the list and add the tuples to the result list if they have not been encountered before.
[(1, 2), (5, 7), (3, 6)]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary Space: O(n), as we are creating a dictionary with n keys.
Method #5: Using recursive function.
[(1, 2), (5, 7), (3, 6)]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary Space: O(n), as we are creating a n function call.
Method #6:Using the dict.fromkeys
[(1, 2), (5, 7), (3, 6)]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using the itertools library
The itertools library provides a function called "groupby" that can be used to group similar items together. We can use this function to group the tuples by their elements, which effectively removes duplicates.
[(1, 2), (3, 6), (5, 7)]
Time complexity: O(n log n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.