S
S
Stepan2016-06-08 20:23:12
Python
Stepan, 2016-06-08 20:23:12

How to find all duplicate elements and their values ​​in a python list?

We need a function that, for example, for the list [10, 10, 23, 10, 123, 66, 78, 123] will return {10: 3, 123: 2}.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YaroslavS, 2016-06-08
@KonstantinovS

Can it be like this

from collections import Counter
my_list = [10, 10, 23, 10, 123, 66, 78, 123]
c = Counter(my_list)
>>> c
Counter({10: 3, 123: 2, 66: 1, 78: 1, 23: 1})
>>> type(c)
<class 'collections.Counter'>
d = dict(c)
>>> d
{10: 3, 123: 2, 66: 1, 78: 1, 23: 1}
>>> type(d)
<class 'dict'>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question