Counter
How to count the elements in a list
With Counter from the collections module (built-in module), you can count the elements of a list. The output is a dictonary where the keys are the elements of the list and the value are the repetitions of those elements in the list.
from collections import Counter
a = [1,2,3,1,1,2,2,3]
b = Counter(a)
b
out[]: Counter({1: 3, 2: 3, 3: 2})
Comments
Post a Comment