Python list count elements

linkaiyi
Follow

Count the occurences of list element

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3
>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})

Emunerate the elements and count is also a good method.

Object has 0 attachments

Was this article helpful?

This article is viewed 15 times!

0 Comments

Leave a Comment

Support