Python List count() method
Python List count() method
The list.count() method returns the number of times x appears in the list.
Syntax
list.count(x)
Parameter
x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.)
Return
This method returns the number of times the specified element appears in the list.
Example 1
# Python program for demonstration # of list.count() method num_list = [11, 12, 13, 14, 11, 11, 11, 14, 15] # printing the number of times 11 appears in the list. print("The count of value 11 is",num_list.count(11)) month_list = ['January', 'Febuary', 'March', 'January', 'May','January'] # printing the number of times January appears in the list. print("The count value of 'January' is",month_list.count('January'))
Output
The count of value 11 is 4 The count value of 'January' is 3
Example 2
# Python program for demonstration # of list.count() method strVal = "Hello World Heelo Hello World" # printing the number of times substring occuring in the string print("The count of Hello: ",strVal.count("Hello"))
Output
The count of Hello: 2