What is the List count() method in Python?

The List.count() method is used to return the number of times an item appears in a list.

Syntax

list.count(value)

Parameter

  • value: This is a required value. It is the value of the item to be searched for in a List.

Return value

The function returns the number of times value appears in the list.

Code

Let’s create a list of random numbers and check for the number of times each item appears on the list.

numbers = [1, 3, 1, 2, 4, 5, 5, 7, 6,2]
# let us return the number of times the number 1 appears on the list
print(f'The number 1 appears {numbers.count(1)} times' )
# let us return the number of times the number 2 appears on the list
print(f'The number 2 appears {numbers.count(2)} times' )
# let us return the number of times the number 3 appears on the list
print(f'The number 3 appears {numbers.count(3)} time' )
# let us return the number of times the number 4 appears on the list
print(f'The number 4 appears {numbers.count(4)} time' )
# let us return the number of times the number 5 appears on the list
print(f'The number 5 appears {numbers.count(5)} times' )
# let us return the number of times the number 6 appears on the list
print(f'The number 6 appears {numbers.count(6)} time' )
# let us return the number of times the number 7 appears on the list
print(f'The number 7 appears {numbers.count(7)} time' )

Explanation

  • Line 1: We created a list of numbers and called it numbers.
  • Line 4: We introduced the .count(1) method to help us count the number of times the number 1 appeared on the list.
  • Lin 6, 8, 10, 12, and 16: we introduced the .count() method to help count or return the number of times every other item (2, 3, 4, 5, 6, and 7) of the list appeared.

Free Resources