Identify duplicate values in a list in Python
Is it possible to get which values are duplicates in a list using python?
I have a list of items:
mylist = [20, 30, 25, 20]
I know the best way of removing the duplicates is set(mylist)
, but is it possible to know what values are being duplicated? As you can see, in this list the duplicates are the first and last values. [0, 3]
.
Is it possible to get this result or something similar in python? I'm trying to avoid making a ridiculously big if elif
conditional statement.
Solution 1:
These answers are O(n), so a little more code than using mylist.count()
but much more efficient as mylist
gets longer
If you just want to know the duplicates, use collections.Counter
from collections import Counter
mylist = [20, 30, 25, 20]
[k for k,v in Counter(mylist).items() if v>1]
If you need to know the indices,
from collections import defaultdict
D = defaultdict(list)
for i,item in enumerate(mylist):
D[item].append(i)
D = {k:v for k,v in D.items() if len(v)>1}
Solution 2:
Here's a list comprehension that does what you want. As @Codemonkey says, the list starts at index 0, so the indices of the duplicates are 0 and 3.
>>> [i for i, x in enumerate(mylist) if mylist.count(x) > 1]
[0, 3]
Solution 3:
The following list comprehension will yield the duplicate values:
[x for x in mylist if mylist.count(x) >= 2]