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 – count()
Following is the syntax of count() function.
n = list.count(item)
Run count() function returns the number of occurrences of the item
in this list
.
Example 1: Count occurrences of Item in Python List
In the following example, we will create a list and find the number of occurrences of some of the items in the list.
Python Program
mylist = [6, 52, 74, 62, 85, 62, 62, 85, 6, 92, 74]
length_74 = mylist.count(74)
length_62 = mylist.count(62)
length_92 = mylist.count(92)
print('74 occurred', length_74, 'times in the list.')
print('62 occurred', length_62, 'times in the list.')
print('92 occurred', length_92, 'times in the list.')
Run Output
74 occurred 2 times in the list.
62 occurred 3 times in the list.
92 occurred 1 times in the list.
The count() function takes exactly one argument.
Summary
In this tutorial of Python Examples, we learned how to count the number of times an item is present in a given list.
Related Tutorials
- Python List of Lists
- Python List without Last Element
- How to Check if Python List is Empty?
- Python Program to Find Unique Items of a List
- How to Get the list of all Python keywords?
- Python Program to Find Smallest Number in List
- How to Reverse Python List?
- Python – Get Index or Position of Item in List
- How to Get List of all Files in Directory and Sub-directories?
- Python – Check if Element is in List