Open In App

Python | Remove duplicate lists in tuples (Preserving Order)

Last Updated : 21 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 that it’s repeated occurrence can be avoided and then this is added to newly maintained unique tuple, removing duplicates.

Python3




# Python3 code to demonstrate working of
# Remove duplicate lists in tuples(Preserving Order)
# Using list comprehension + set()
 
# Initializing tuple
test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Remove duplicate lists in tuples(Preserving Order)
# Using list comprehension + set()
temp = set()
res = [ele for ele in test_tup if not(tuple(ele) in temp or temp.add(tuple(ele)))]
 
# printing result
print("The unique lists tuple is : " + str(res))


Output : 

The original tuple is : ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
The unique lists tuple is : [[4, 7, 8], [1, 2, 3], [9, 10, 11]]

 

Time complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using OrderedDict() + tuple() 
The combination of above functions can also be used to perform this particular task. In this, we convert the tuple to OrderedDict(), which automatically removes the duplicate elements and then construct a new tuple list using tuple().

Python3




# Python3 code to demonstrate working of
# Remove duplicate lists in tuples(Preserving Order)
# Using OrderedDict() + tuple()
from collections import OrderedDict
 
# Initializing tuple
test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Remove duplicate lists in tuples(Preserving Order)
# Using OrderedDict() + tuple()
res = list(OrderedDict((tuple(x), x) for x in test_tup).values())
 
# printing result
print("The unique lists tuple is : " + str(res))


Output : 

The original tuple is : ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
The unique lists tuple is : [[4, 7, 8], [1, 2, 3], [9, 10, 11]]

 

Method #3: Using recursive method.

Python3




# Python3 code to demonstrate working of
# Remove duplicate lists in tuples(Preserving Order)
# Using Recursive method
# Recursive function to remove duplicate lists in a tuple
def remove_duplicates(tup, result, seen):
    # Base case: if the tuple is empty, return the result
    if not tup:
        return result
     
    # If the current list is not in the "seen" set, append it to the result
    # and add it to the "seen" set
    if tuple(tup[0]) not in seen:
        result.append(tup[0])
        seen.add(tuple(tup[0]))
    # Recursively call the function with the rest of the tuple
    return remove_duplicates(tup[1:], result, seen)
 
# Initialize the tuple
test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
 
# Call the function with an empty result list and an empty set
result = []
seen = set()
res = remove_duplicates(test_tup, result, seen)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
# printing result
print("The unique lists tuple is : " + str(res))
 
 
#this code contributed by tvsk


Output

The original tuple is : ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
The unique lists tuple is : [[4, 7, 8], [1, 2, 3], [9, 10, 11]]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4:Using a loop and a set:

Algorithm:

1.Create an empty list called “result” and an empty set called “seen”.
2.For each sublist in the input tuple:
a. Convert the sublist to a tuple called “t”.
b. If “t” is not in the “seen” set, append the original sublist to the “result” list and add “t” to the “seen” set.
3.Convert the “result” list to a tuple and return it.

Python3




def remove_duplicates(tup):
    result = []
    seen = set()
    for sublist in tup:
        t = tuple(sublist)
        if t not in seen:
            result.append(sublist)
            seen.add(t)
    return tuple(result)
 
# Example usage
test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
unique_tup = remove_duplicates(test_tup)
 
print("Original tuple:", test_tup)
print("Tuple with duplicate lists removed:", unique_tup)
#This code is contributed by Jyothi pinjala


Output

Original tuple: ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
Tuple with duplicate lists removed: ([4, 7, 8], [1, 2, 3], [9, 10, 11])

Time complexity: O(n*m) where n is the number of sublists in the input tuple and m is the length of the largest sublist. This is because we need to iterate over each sublist and create a tuple from it, and checking membership in a set takes constant time on average.

Auxiliary Space: O(n*m) because we need to store the input tuple, the output list, and the set of seen tuples. In the worst case where all sublists are unique, the size of the output list will be equal to the size of the input tuple, so the space complexity will be proportional to the size of the input tuple.

Method #5: Using loop and if-else statement

Algorithm:

  1. Initialize an empty list res to store unique lists.
  2. Loop through each list i in the given tuple test_tup.
  3. If the list i is not already in the res list, then append i to the res list.
  4. Return the res list as the result.

Python3




test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
res = []
for i in test_tup:
    if i not in res:
        res.append(i)
print("The unique lists tuple is : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The unique lists tuple is : [[4, 7, 8], [1, 2, 3], [9, 10, 11]]

Time Complexity:

The loop runs n times, where n is the length of the input tuple.
The list append operation and the list membership test both have O(1) time complexity on average.
Therefore, the overall time complexity of the algorithm is O(n) on average.
Auxiliary Space:

The space used by the res list is O(k), where k is the number of unique lists in the input tuple.
The space used by other variables is constant.
Therefore, the overall space complexity of the algorithm is O(k).

Method 6: Using generator function

Define a generator function that takes a tuple as input.
Initialize an empty set to store the seen tuples.
Loop through the original tuple.
Convert the current list to a tuple and check if it is already in the set.
If it is not in the set, yield the current list and add the tuple to the set.
Return the generator function.

Python3




# Python3 code to demonstrate working of
# Remove duplicate lists in tuples(Preserving Order)
# Using Generator function
 
# Generator function to remove duplicate lists in a tuple
def remove_duplicates(tup):
    seen = set()
    for lst in tup:
        tup_lst = tuple(lst)
        if tup_lst not in seen:
            yield lst
            seen.add(tup_lst)
 
# Initialize the tuple
test_tup = ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
 
# Call the generator function to get the unique lists tuple
res = tuple(remove_duplicates(test_tup))
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
# printing result
print("The unique lists tuple is : " + str(res))


Output

The original tuple is : ([4, 7, 8], [1, 2, 3], [4, 7, 8], [9, 10, 11], [1, 2, 3])
The unique lists tuple is : ([4, 7, 8], [1, 2, 3], [9, 10, 11])

This method has a time complexity of O(n), where n is the length of the input tuple, since we only loop through the tuple once.

This method has an auxiliary space complexity of O(n), where n is the length of the input tuple, since we need to store the unique tuples in a set.



Similar Reads

Python | Remove duplicate tuples from list of tuples
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
5 min read
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 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 | 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 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 | 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
Python | How to Concatenate tuples to nested tuples
Sometimes, while working with tuples, we can have a problem in which we need to convert individual records into a nested collection yet remaining as separate element. Usual addition of tuples, generally adds the contents and hence flattens the resultant container, this is usually undesired. Let's discuss certain ways in which this problem is solved
6 min read
Practice Tags :