Python – Remove all duplicate occurring tuple records

Sometimes, while working with records, we can have a problem of removing 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 iterate on each tuple and check it’s count in list using count(), if greater than one, we can remove them, converting then to set.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove all Duplicate occurring records
# Using list comprehension + set() + count()
  
# initialize list
test_list = [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Remove all Duplicate occurring records
# Using list comprehension + set() + count()
res = list(set([ele for ele in test_list if not test_list.count(ele) > 1]))
  
# printing result
print("All the non Duplicate from list are : " + str(res))

chevron_right


Output :

The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(6, 7), (3, 6)]

 

Method #2 : Using Counter() + items() + list comprehension
The combination of above functions can also be used to perform this particular task. In this, we just get the count of each occurrence of element using Counter() as dictionary and then extract all those whose value is equal to 1.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove all Duplicate occurring records
# Using list comprehension + Counter() + items()
from collections import Counter
  
# initialize list
test_list = [(3, 4), (4, 5), (3, 6), (3, 4), (4, 5), (6, 7)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Remove all Duplicate occurring records
# Using list comprehension + Counter() + items()
res = [ele for ele, count in Counter(test_list).items() if count == 1]
  
# printing result
print("All the non Duplicate from list are : " + str(res))

chevron_right


Output :

The original list : [(3, 4), (4, 5), (3, 4), (3, 6), (4, 5), (6, 7)]
All the non Duplicate from list are : [(6, 7), (3, 6)]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.