Python list count() method “returns the number of times provided element appears in the list.”
Syntax
list.count(element)
Parameters
element: The count() function takes a single argument called an element, the element to be counted.
Return Value
The count() function returns the number of times an element appears in the list.
Example 1: How to Use list count() Method
# SUPERBOWL list
data = ["S", "U", "P", "E", "R", "B", "O", "W", "L"]
# count element "P"
count = data.count("P")
# print the count
print('The count of P is:', count)
# count element "B"
count = data.count("B")
# print the count
print('The count of B is:', count)
Output
The count of P is: 1
The count of B is: 1
The data is a list containing SUPERBOWL LETTERS.
Example 2: Count the specific integer
Let’s define a list of integers and count the specific integer element using the count() method.
# Integers list
ints = [11, 21, 19, 29, 46, 21, 19]
# count element 21
count = ints.count(21)
# print the count
print('The count of 21 is:', count)
# count element 19
count = ints.count(19)
# print the count
print('The count of 19 is:', count)
Output
The count of 21 is: 2
The count of 19 is: 2
Example 3: Count Tuple and List Elements Inside List
Let’s define a list of single elements and tuples and find its count using the count() method.
# Integers list
list_of_tup = [(11, 21), (19, 29), (46), 21, 19]
# count element (11, 21)
count = list_of_tup.count((11, 21))
# print the count
print('The count of (11, 21) is:', count)
# count element 21
count = list_of_tup.count(21)
# print the count
print('The count of 21 is:', count)
Output
The count of (11, 21) is: 1
The count of 21 is: 1
That’s it.