How to count all duplicate elements in a list using Python

1 Answer

0 votes
import collections

lst = [1, 1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7]

count_elements = {i:lst.count(i) for i in lst}

for key, value in count_elements.items():
  if (value > 1):
    print(key, " : ", value)


'''
run:

1  :  2
4  :  3
6  :  2
7  :  4

'''

 



answered Feb 18, 2019 by avibootz

Related questions

3 answers 256 views
1 answer 175 views
1 answer 171 views
1 answer 146 views
1 answer 198 views
2 answers 236 views
1 answer 210 views
...