Python 3 reference · List Methods
Python List count() Method
Return how many times a value appears in the list, comparing each item with ==. Returns 0 if it never appears.
Python 3 Since 3.0
Syntax
list.count(value)
Parameters
| Parameter | Description |
|---|---|
value |
The value to count. |
Return value
An int.
Examples
votes = ["yes", "no", "yes", "yes"]
print(votes.count("yes"), votes.count("maybe"))
Output
3 0
from collections import Counter
print(Counter(["a", "b", "a", "c", "a"]).most_common(2))
Output
[('a', 3), ('b', 1)]Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list.index()· Return the position of the first item equal to a value. Raises ValueError if the value isn't in the list.str.count()· Return the number of non-overlapping occurrences of a substring, optionally only between start and end positions.- Python Lists · tutorial