Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

list.count

Counts elements that are the supplied object or compare equal to it.

Version: Python 3.14; repository image, production runtime not attested

Syntax

a.count(value)

Counts elements that are the supplied object or compare equal to it.

Equal Boolean, integer and floating-point values contribute to the same count. A missing value gives zero.

Counts identical objects or equal items, regardless of whether their types are identical. The integer 1, Boolean True and floating-point 1.0 all compare equal to one, giving three; none compares equal to seven, giving zero.

x = float("nan")
assert x != x and [x].count(x) == 1 and [x].count(float("nan")) == 0

Examples

a = [1, True, 1.0, 9]
print(a.count(1))
print(a.count(7))

Result

3
0

Counts identical objects or equal items, regardless of whether their types are identical. The integer 1, Boolean True and floating-point 1.0 all compare equal to one, giving three; none compares equal to seven, giving zero.

Sources