Python list Method - count()

The count() method in Python is a built-in method used to count the number of times a specified element occurs in a list.

Syntax:

list_name.count(element)
Source:‮tfigi.www‬idea.com

Example:

my_list = [1, 2, 3, 4, 1, 2, 3, 1]
count_ones = my_list.count(1)
print(count_ones)   # Output: 3

In the above example, the count() method is used to count the number of times the element 1 occurs in the list my_list. The method returns an integer value that represents the count of the specified element in the list.

If the specified element is not present in the list, the count() method returns 0. If the list contains mutable objects like lists, dictionaries, etc., then the method only counts the number of times the object appears in the list, not the number of times an object with the same value appears in the list.