Python Glossary Русский

all Python 3Lists (list)function

list.count

search

Return the number of occurrences of a value in the list (compared with ==).

Syntax

lst.count(value)

Every call rescans the whole list, so counting frequencies by looping over the elements and calling count() each time is quadratic — collections.Counter does the same work in a single pass. Matching uses ==, not the type, so True is indistinguishable from 1 and False from 0, which can surprise you in a mixed list. It tells you how many, never where — that is index()'s job.

Examples

nums = [1, 2, 2, 3, 2]
print(nums.count(2))   # → 3
print(nums.count(9))   # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy