Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 892
Hairo Avatar asked Jun 27 '12 23:06

Hairo


People also ask

How do you find the number of repeated values in a list in Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

Can a list have duplicate values in Python?

Python list can contain duplicate elements.

Can you have duplicate values in a list?

What are duplicates in a list? If an integer or string or any items in a list are repeated more than one time, they are duplicates.


1 Answers

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} 
like image 106
John La Rooy Avatar answered Sep 28 '22 23:09

John La Rooy