Python List count()

The list count() method is used to count the occurrences of a specified element in a list.

Syntax

list.count(element)

Parameter

element : (Required) The item you want to count in the list.

Return Value

The count() method returns an integer representing the number of times the element appears in the list. It returns 0 if the specified item is not found in the list.

Example 1: Counting occurrences of an element in a list

my_list = [5, 10, 5, 20]

# Count the number of times the value 5 appears in the list
count = my_list.count(5)
print(count) # Output: 2

If the specified element doesn’t exist in the list, the count() method returns 0.

my_list = [5, 10, 15, 20]

count = my_list.count(25)
print(count) # Output: 0