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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 #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 autoatically removes the duplicate elemets and then construct a new tuple list using tuple().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.