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 Program to Find Largest Number in a List
- Python – List of Strings
- Python Program to Find Unique Items of a List
- How to Check if Python List is Empty?
- Python List without Last Element
- Python List with First N Elements
- How to Get List of all Files in Directory and Sub-directories?
- Python – How to Create an Empty List?
- How to Insert Item at Specific Index in Python List?
- Python Program to Find Smallest Number in List