Open In App

Python | Remove duplicate tuples from list of tuples

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 program to remove duplicate
# tuples from list of tuples
 
def removeDuplicates(lst):
     
    return [t for t in (set(tuple(i) for i in lst))]
         
# Driver code
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




# Python3 program to remove duplicate
# tuples from list of tuples
 
def removeDuplicates(lst):
     
    return list(set([i for i in lst]))
         
# Driver code
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




# Python3 program to remove duplicate
# tuples from list of tuples
 
def removeDuplicates(lst):
     
    return [[a, b] for i, [a, b] in enumerate(lst)
    if not any(c == b for _, c in lst[:i])]
         
# Driver code
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):
    # Create an empty dictionary to store tuples as keys
    encountered = {}
    # Create an empty list to store unique tuples
    result = []
    # Iterate through the input list
    for tup in lst:
        # If the tuple has not been encountered before,
        # add it to the dictionary and the result list
        if tup not in encountered:
            encountered[tup] = True
            result.append(tup)
    # Return the list of unique tuples
    return result
#Driver code
lst = [(1, 2), (5, 7), (3, 6), (1, 2)]
print(remove_duplicates(lst))
#This code is contributed by Edula Vinay Kumar Reddy


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




#defining a recursive function to remove duplicate tuple in a list
def remove_duplicate(start,oldlist,newlist):
    if start==len(oldlist):return newlist  #base condition
    if oldlist[start] not in newlist:   #checking whether element present in new list  or not
        newlist.append(oldlist[start])
    return remove_duplicate(start+1,oldlist,newlist)  #recursive call
   
   
#driver code
lst = [(1, 2), (5, 7), (3, 6), (1, 2)]    #Initaizing tuples in a list
res=remove_duplicate(0,lst,[])   #calling recursive function with correct parameters
print(res)  #printing result


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):
    # Convert the list of tuples to a dictionary using dict.fromkeys
    # This automatically removes duplicates because dict keys must be unique
    dict_without_duplicates = dict.fromkeys(lst)
     
    # Return the list of keys from the dictionary, which will be the original tuples
    return list(dict_without_duplicates.keys())
 
# Example usage
lst = [(1, 2), (5, 7), (3, 6), (1, 2)]
print(removeDuplicates(lst))
#This code is contributed by Vinay Pinjala.


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):
    # Sort the list of tuples to ensure duplicates are grouped together
    lst.sort()
    # Use itertools.groupby to group the tuples by their elements
    # This effectively removes duplicates
    grouped = itertools.groupby(lst)
    # Convert the grouped object back to a list of tuples
    # We only need the keys (tuples) since the values are just placeholders
    unique = [key for key,_ in grouped]
    return unique
 
# Example usage
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), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
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 list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove duplicate lists in tuples (Preserving Order)
Sometimes, while working with records, we can have a problem in which we need to remove duplicate records. This kind of problem is common in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() In this method, we test for each list as it appears and add it to set so tha
7 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. Example: Input : test_list = [(6, 9, 17, 18), (15, 34, 5
6 min read
Python Program to Remove duplicate tuples irrespective of order
Given a list of binary tuples, the task is to write a Python program to remove all tuples that are duplicates irrespective of order, i.e delete if contains similar elements, irrespective of order. Input : test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]Output : [(1, 2), (5, 7), (4, 6), (2, 9)]Explanation : (2, 1), (6, 4) are rem
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() Initial approach that can be applied is that we can
7 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read
Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read