Python | Remove duplicate tuples from list of tuples
Last Updated :
16 Mar, 2023
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.
Python3
def removeDuplicates(lst):
return [t for t in ( set ( tuple (i) for i in lst))]
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (removeDuplicates(lst))
|
Output:
[(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.
Python3
def removeDuplicates(lst):
return list ( set ([i for i in lst]))
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (removeDuplicates(lst))
|
Output:
[(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
Python3
def removeDuplicates(lst):
return [[a, b] for i, [a, b] in enumerate (lst)
if not any (c = = b for _, c in lst[:i])]
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (removeDuplicates(lst))
|
Output:
[[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.
Python3
def remove_duplicates(lst):
encountered = {}
result = []
for tup in lst:
if tup not in encountered:
encountered[tup] = True
result.append(tup)
return result
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (remove_duplicates(lst))
|
Output
[(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.
Python3
def remove_duplicate(start,oldlist,newlist):
if start = = len (oldlist): return newlist
if oldlist[start] not in newlist:
newlist.append(oldlist[start])
return remove_duplicate(start + 1 ,oldlist,newlist)
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
res = remove_duplicate( 0 ,lst,[])
print (res)
|
Output
[(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
Python3
def removeDuplicates(lst):
dict_without_duplicates = dict .fromkeys(lst)
return list (dict_without_duplicates.keys())
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (removeDuplicates(lst))
|
Output
[(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.
Python3
import itertools
def removeDuplicates(lst):
lst.sort()
grouped = itertools.groupby(lst)
unique = [key for key,_ in grouped]
return unique
lst = [( 1 , 2 ), ( 5 , 7 ), ( 3 , 6 ), ( 1 , 2 )]
print (removeDuplicates(lst))
|
Output
[(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.
Similar Reads
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), (
7 min read
Python | Remove all strings from a list of tuples
Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
8 min read
Remove Duplicate Strings from a List in Python
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values. [GFGTABS] Python a = ["Learn
3 min read
Python Remove Duplicates from a List
Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Python | Removing duplicates from tuple
Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python - Remove Duplicate subset Tuples
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of tuples, which are already present as subsets in other tuples. This kind of problem can be useful in data preprocessing. Let's discuss certain ways in which this task can be performed. Exampl
6 min read
Python | Get duplicate tuples from list
Sometimes, while working with records, we can have a problem of extracting those records which occur more than once. This kind of application can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() In
7 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial lis
6 min read
Python - Flatten tuple of List to tuple
The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Python - Remove duplicate words from Strings in List
Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read