Python list count() method
Count how many items are equal to a value.
Syntax
list.count(value)
To count every distinct value at once, use collections.Counter.
Parameters
| value | what to count |
Return value
int
Example
Code
votes = ["tea", "coffee", "tea", "tea"] print(votes.count("tea"), votes.count("juice")) from collections import Counter print(Counter(votes))
Output
3 0
Counter({'tea': 3, 'coffee': 1})
Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.