Hairo
Hairo

Reputation: 2214

get max duplicate item indexes in a list using python

As someone here pointed me out, for getting the max duplicated item in a list this can be used:

>>> from collections import Counter
>>> mylist = [20, 20, 25, 25, 30, 30]
>>> max(k for k,v in Counter(mylist).items() if v>1)
30

but, what if i want to get the indexes instead of the values, being here [4, 5]

any help??

Regards...

Upvotes: 1

Views: 1150

Answers (1)

jamylak
jamylak

Reputation: 133514

>>> from collections import defaultdict
>>> mylist = [20, 20, 25, 25, 30, 30]
>>> D = defaultdict(list)
>>> for i,x in enumerate(mylist):
        D[x].append(i)


>>> D[max(k for k,v in D.items() if len(v)>1)]
[4, 5]

Upvotes: 3

Related Questions