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)
count() function returns the number of occurrences of the
item in this
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.
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.')
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.
In this tutorial of Python Examples, we learned how to count the number of times an item is present in a given list.
- Python List of Dictionaries
- How to Reverse Python List?
- Python List of Functions
- Python List of Lists
- How to Access List Items in Python?
- Python Program to Find Largest Number in a List
- Python Program to Find Smallest Number in List
- Shuffle Python List
- Python List – Add Item
- Python – Get Index or Position of Item in List