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 iterate on each tuple and check it’s count in list using count(), if greater than one, we can add to list. To remove multiple additions, we can convert the result to set using set().

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



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

 

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 above 1.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


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.