Get a unique list of items that appear more than once in the list
I have a list of items:
mylist = ['A','A','B','C','D','E','D']
I want to return a unique list of items that appear more than once in my list, so my desired result is:
[A,D]
Not sure what to do, but my process is to first add the count of each item and then remove everything equal to 1. Then dedup, but this seems like a really roundabout, inefficient way to do it, so I'm looking for advice.
+3
source to share
7 replies
You can use collections.Counter
to do what you described easily:
from collections import Counter
mylist = ['A','A','B','C','D','E','D']
cnt = Counter(mylist)
print [k for k, v in cnt.iteritems() if v > 1]
# ['A', 'D']
+7
source to share
Try the following:
a = ['A','A','B','C','D','E','D']
import collections
print [x for x, y in collections.Counter(a).items() if y > 1]
['A', 'D']
Ref: How to find duplicate elements in an array using for loop in Python?
OR
def list_has_duplicate_items( mylist ):
return len(mylist) > len(set(mylist))
def get_duplicate_items( mylist ):
return [item for item in set(mylist) if mylist.count(item) > 1]
mylist = [ 'oranges' , 'apples' , 'oranges' , 'grapes' ]
print 'List: ' , mylist
print 'Does list have duplicate item(s)? ' , list_has_duplicate_items( mylist )
print 'Redundant item(s) in list: ' , get_duplicate_items( mylist )
Link https://www.daniweb.com/software-development/python/threads/286996/get-redundant-items-in-list
+1
source to share