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 224 views
1 answer 162 views
1 answer 158 views
1 answer 134 views
1 answer 186 views
2 answers 213 views
...