Search code examples
pythonarrayslist

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

  • 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}