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.
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.
Python list can contain duplicate elements.
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.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With