Updated March 28, 2023
Introduction to Python List count
The following article provides an outline for Python List count. Count() is an in-built function in Python that returns the counts or, in many simple terms, how many times a given object occurs in the list. In other words, it returns the frequency of occurrence of that object within the list we are searching for. If we are looking forward to a particular column’s mode in a specified Dataset, then the count function can be utilized to take care of the same by implementing this column as a list.
Syntax:
The syntax of the Python count() function to return the frequency of an object is as below:
list.count(Object)
- The object is the numeral, string or any other whose count is to be returned.
- Count() method returns the count of how many times the object occurs in the list.
How does Python List count Work?
- Here we started by creating a list of integers with each element having a single of multiple occurrences.
- Count() function is used as an attribute to the list1, along with an argument.
- Here the argument passed is an element whose occurrence we need to check within the list.
- The returned value is the integer which represents the counts of the element passed as an argument to the function.
Examples of Python List count
Given below are the examples mentioned:
Example #1
Code:
## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 ,5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times the numeral 6 occours in the list1
print(list1.count(6))
Output:
What happens if we need to check the occurrences of two elements in a single go. Do we pass two elements separated by a comma to the count() function?
Let’s check this out by taking another example on the same.
Example #2
Code:
## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 , 5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count(6,1))
Output:
This program throws an error when passed two elements to check the occurrences of the same in the list. So how can we check the occurrences of multiple elements in the same list?
Yes, we need to call the count() function on the same list again, bypassing the other elements.
Let’s check this out by taking another example on the same.
Example #3
Code:
## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 , 5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count(6))
print(list1.count(1))
Output:
How does the count function behave with the other data types like char or strings etc.?
Let’s explore the same by taking some examples.
Example #4
Code:
## Python program to count the number of times an object occurs in the list of chars
list1 = [ 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' ,'e' , 'r' , 't' ,'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' ]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count('e'))
print(list1.count('z'))
Output:
Notice here in the second print, where we have passed ‘z’ as an element to check its occurrence in the list.
We get an output of 0 for this iteration. The reason being ‘z’ is not even a part of the list. Let’s explore the same by taking some more examples of how it works for strings.
Example #5
Code:
## Python program to count the number of times an object occurs in the list of string
list1 = [ 'edu' , 'cba' , 'learn' , 'ca' , 'na' ]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count('edu'))
Output:
Conclusion
Pythons List count() function returns the number of times the element is present in the list. We cannot pass multiple elements in the list to check for their occurrences. If we need to do the same, we need to pass the elements separate in separate count functions.
Recommended Articles
This is a guide to Python List count. Here we discuss a brief overview of Python List count and its examples, along with its code implementation. You can also go through our other suggested articles to learn more –