18

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 mylist, so that my desired output would be:

 [A,D]

Not sure how to even being this, but my though process is to first append a count of each item, then remove anything equal to 1. Then dedupe, but this seems like a really roundabout, inefficient way to do it, so I am looking for advice.

user2242044
  • 8,803
  • 25
  • 97
  • 164
  • possible duplicate of [How to find duplicate elements in array using for loop in python like c/c++?](http://stackoverflow.com/questions/1920145/how-to-find-duplicate-elements-in-array-using-for-loop-in-python-like-c-c) – Avinash Raj Nov 06 '14 at 06:39

10 Answers10

17

You can use collections.Counter to do what you have 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']
YS-L
  • 14,358
  • 3
  • 47
  • 58
7
>>> mylist = ['A','A','B','C','D','E','D']
>>> set([i for i in mylist if mylist.count(i)>1])
set(['A', 'D'])
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
  • For short lists, this is a useful and easily understandable approach. But keep in mind: this solution has quadratic complexity (n*n). The solutions using collections.Counter have a linear (2*n) complexity. – Alex Feb 09 '23 at 13:48
2
import collections
cc = collections.Counter(mylist) # Counter({'A': 2, 'D': 2, 'C': 1, 'B': 1, 'E': 1})
cc.subtract(cc.keys())           # Counter({'A': 1, 'D': 1, 'C': 0, 'B': 0, 'E': 0})
cc += collections.Counter()      # remove zeros (trick from the docs)
print cc.keys()                  # ['A', 'D']
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2

Try some thing like this:

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

Reference: How to find duplicate elements in 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 )

Reference https://www.daniweb.com/software-development/python/threads/286996/get-redundant-items-in-list

Community
  • 1
  • 1
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
2

Using a similar approach to others here, heres my attempt:

from collections import Counter

    def return_more_then_one(myList):
         counts = Counter(my_list)
         out_list = [i for i in counts if counts[i]>1]
         return out_list
NevDev
  • 604
  • 5
  • 8
2

It can be as simple as ...

print(list(set([i for i in mylist if mylist.count(i) > 1])))
Sravan K Ghantasala
  • 1,058
  • 8
  • 14
1

Use set to help you do that, like this maybe :

X = ['A','A','B','C','D','E','D']
Y = set(X)
Z = []

for val in Y :
    occurrences = X.count(val)
    if(occurrences > 1) :
        #print(val,'occurs',occurrences,'times')
        Z.append(val)

print(Z)

The list Z will save the list item which occur more than once. And the part I gave comment (#), that will show the number of occurrences of each list item which occur more than once

ahmfarisi
  • 154
  • 8
0

Might not be as fast as internal implementations, but takes (almost) linear time (since set lookup is logarithmic)

mylist = ['A','A','B','C','D','E','D']
myset = set()
dups = set()
for x in mylist:
    if x in myset:
        dups.add(x)
    else:
        myset.add(x)
dups = list(dups)
print dups
saeedgnu
  • 4,110
  • 2
  • 31
  • 48
0

another solution what's written:

def delete_rep(list_):
new_list = []
for i in list_:
    if i not in list_[i:]:
        new_list.append(i)

return new_list
mtbenj
  • 11
  • 2
0

This is my approach without using packages

result = []
for e in listy:
    if listy.count(e) > 1:
        result.append(e)
    else:
        pass
print(list(set(result)))
rad15f
  • 29
  • 3