Python List count() Method

The list.count() method returns the number of times an element occurs in the list.

Syntax:

list.count(item)

Parameter:

item: (Required) The item to be counted in the list.

Return Value:

An integer.

The following example demonstrates the count() method.

Example: count()
city = 'Mumbai'
cities = ['Mumbai', 'London', 'Paris', 'New York','Mumbai']
num = cities.count(city)
print("The number of times Mumbai occurs is: ", num)
Output
The number of times Mumbai occurs is: 2

The count() method can also be used to count an iterable within an iterable.

Example: count()
cities = [('Mumbai','Delhi'), 'London', 'Paris', 'New York']
indianCities = ('Mumbai','Delhi')
num = cities.count(indianCities)
print("The number of times tuple occurs in the list is: ", num)
Output
The number of times tuple occurs in the list is:  1

The count() method returns 0 if the element is not found in the list.

Example: count()
num = 5
oddNums = [1, 3, 7, 11, 51, 23]
count = oddNums.count(num)
print("Number of times 5 appears is: ", count)
Output
Number of times target appears is:  0

The count() method will throw an error if no parameter passed.

Example: count()
oddNums = [1, 3, 7, 11, 51, 23]
count = oddNums.count() # raise an error

print("Number of times 5 appears is: ", count)
Output
Traceback (most recent call last):
    oddNums.count()
TypeError: count() takes exactly one argument (0 given)