Python List count() method returns the count of the occurrences of a given element in a list.
Example:
#create a list fruits = [ "Apple" , "Mango" , "Banana" , "Cherry" , "Papaya" ]
# printing count using count() function print (fruits.count( "Apple" ))
|
1
What is the list count() Method?
list count() function in Python is a built-in function that lets you count the occurrence of an element in a list. It returns the count of how many times an element is present in a list.
It has various applications depending on how you use it. For example: If the count of any element is greater than 1 that means there are duplicate values. If count is 0 that means the element is not present in the list. So depending on how you use it, it can have different uses.
It only accepts one parameter, if you pass more than one parameter it raises TypeError.
List count() Method Syntax
list_name.count(object)
Parameters:
- object: is the item whose count is to be returned.
Returns:
Returns the count of how many times an object occurs in the list.
How to Use List count() Function
The list count() function is a very easy-to-use function, you just need to call the count() function with the object list and pass the element as a parameter in the function.
Let’s understand better how to count the occurrence of an element in the list with a simple example:
#creating a list Rand = [ 1 , 3 , 2 , 4 , 1 , 3 , 2 , 4 , 5 , 2 , 3 ]
#lets count occurence of 2 print (Rand.count( 2 ))
|
3
More Examples on List count() Method
Let’s discuss some of the examples in different use-cases of count() method.
Example:
list2 = [ 'a' , 'a' , 'a' , 'b' , 'b' , 'a' , 'c' , 'b' ]
print (list2.count( 'b' ))
|
3
Count Tuple and List Elements Inside the List
Count occurrences of List and Python Tuples inside a list using the Python count() method.
list1 = [ ( 'Cat' , 'Bat' ), ( 'Sat' , 'Cat' ), ( 'Cat' , 'Bat' ),
( 'Cat' , 'Bat' , 'Sat' ), [ 1 , 2 ], [ 1 , 2 , 3 ], [ 1 , 2 ] ]
# Counts the number of times 'Cat' appears in list1 print (list1.count(( 'Cat' , 'Bat' )))
# Count the number of times sublist # '[1, 2]' appears in list1 print (list1.count([ 1 , 2 ]))
|
2 2
Exceptions While Using Python list count() method
Let’s also discuss some errors you might face while using the count() function.
TypeError: count() takes exactly one argument
List count() in Python raises TypeError when more than 1 parameter is passed.
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 ]
# Error when two parameters is passed. print (list1.count( 1 , 2 ))
|
Output:
Traceback (most recent call last):
File "/home/41d2d7646b4b549b399b0dfe29e38c53.py", line 7, in
print(list1.count(1, 2))
TypeError: count() takes exactly one argument (2 given)
Practical Application
Let’s say we want to count each element in a Python list and store it in another list or say Python dictionary.
# Python3 program to count the number of times # an object appears in a list using count() method lst = [ 'Cat' , 'Bat' , 'Sat' , 'Cat' , 'Mat' , 'Cat' , 'Sat' ]
# To get the number of occurrences # of each item in a list print ([ [l, lst.count(l)] for l in set (lst)])
# To get the number of occurrences # of each item in a dictionary print ( dict ( (l, lst.count(l) ) for l in set (lst)))
|
[['Mat', 1], ['Sat', 2], ['Bat', 1], ['Cat', 3]] {'Mat': 1, 'Sat': 2, 'Bat': 1, 'Cat': 3}
We covered the definition, syntax, and examples of the list count() method. We also discussed different exceptions and practical examples of the function.
The list count() method is a very basic function of list operations and it is very easy to use.
Read More: Python List Methods
Also Read:
- Count occurrences of an element in a list
- How to Get the Number of Elements in a Python List?
- Find all elements count in list
- Find most frequent element in a list