Python List count() method
Last Updated :
20 Dec, 2023
Python List count() method returns the count of the occurrences of a given element in a list.
Example:
Python3
fruits = [ "Apple" , "Mango" , "Banana" , "Cherry" , "Papaya" ]
print (fruits.count( "Apple" ))
|
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:
Python3
Rand = [ 1 , 3 , 2 , 4 , 1 , 3 , 2 , 4 , 5 , 2 , 3 ]
print (Rand.count( 2 ))
|
More Examples on List count() Method
Let’s discuss some of the examples in different use-cases of count() method.
Example:
Python3
list2 = [ 'a' , 'a' , 'a' , 'b' , 'b' , 'a' , 'c' , 'b' ]
print (list2.count( 'b' ))
|
Count Tuple and List Elements Inside the List
Count occurrences of List and Python Tuples inside a list using the Python count() method.
Python3
list1 = [ ( 'Cat' , 'Bat' ), ( 'Sat' , 'Cat' ), ( 'Cat' , 'Bat' ),
( 'Cat' , 'Bat' , 'Sat' ), [ 1 , 2 ], [ 1 , 2 , 3 ], [ 1 , 2 ] ]
print (list1.count(( 'Cat' , 'Bat' )))
print (list1.count([ 1 , 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.
Python3
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 ]
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
lst = [ 'Cat' , 'Bat' , 'Sat' , 'Cat' , 'Mat' , 'Cat' , 'Sat' ]
print ([ [l, lst.count(l)] for l in set (lst)])
print ( dict ( (l, lst.count(l) ) for l in set (lst)))
|
Output
[['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:
Please Login to comment...