Contents
Count Items in List with a Given Value
To count the number of occurrences of items with a specific value in a list, you can call count() function on the list with the specific item passed as argument to the count() function.
Syntax of count()
Following is the syntax of count() function.
count() function returns the number of occurrences of the item in this list.
Examples
1. Count occurrences of the item “74” in given list
In the following example, we take a list of numbers in mylist, and find the number of occurrences of the item 74 in the list.
Python Program
mylist = [6, 74, 62, 85, 62, 62,74]
item = 74
n = mylist.count(item)
print(f'{item} occurred {n} times in the list.')
Run Code CopyOutput
74 occurred 2 times in the list.
The count() function takes exactly one argument.
2. Count occurrences of the item “apple” in given list
In the following example, we take a list of strings in mylist, and find the number of occurrences of the item "apple" in the list.
Python Program
mylist = ['apple', 'banana', 'cherry', 'apple', 'apple']
item = 'apple'
n = mylist.count(item)
print(f'{item} occurred {n} times in the list.')
Run Code CopyOutput
apple occurred 3 times in the list.
Summary
In this tutorial of Python Examples, we learned how to count the number of times an item is present in a given list.