How to count all 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():
    print(key, " : ", value)


'''
run:

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

'''

 



answered Feb 18, 2019 by avibootz
...