list.count
Count how many times the value x occurs in the list.
Python 3.13
list.count(x)Parameters
- xAny
- The value to count.
Return value
An integer; 0 if the value is not in the list.
Examples
How many times an item occurs
Python 3.13
print([1, 2, 2, 3].count(2))2
True and 1.0 equal 1
Python 3.13
print([1, True, 1.0, '1'].count(1))3
Details
Items are compared with ==, so True and 1.0 count as equal to 1. To count all items in one pass, collections.Counter is more convenient.
