Open In App

Python List count() method

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python List count() method returns the count of the occurrences of a given element in a list.

Example:

Python3




#create a list
fruits = ["Apple", "Mango", "Banana", "Cherry" , "Papaya"]
# printing count using count() function
print(fruits.count("Apple"))


Output

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:

Python3




#creating a list
Rand = [1,3,2,4,1,3,2,4,5,2,3]
#lets count occurence of 2
print(Rand.count(2))


Output

3

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'))


Output

3

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] ]
 
# 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]))


Output

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.

Python3




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




# 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)))


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:



Previous Article
Next Article

Similar Reads

Python - Create nested list containing values as the count of list items
Given a list, the task is to write a Python program to create a nested list where the values are the count of list items. Examples: Input: [1, 2, 3] Output: [[1], [2, 2], [3, 3, 3]] Input: [4, 5] Output: [[1, 1, 1, 1], [2, 2, 2, 2, 2]] Method 1: Using nested list comprehension The list will contain the count of the list items for each element e in
2 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
Count set bits using Python List comprehension
Write an efficient program to count number of 1s in binary representation of an integer. Examples: Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 11 Output : 3 Binary representation of 11 is 1101 and has 3 set bits We have existing solution for this problem please refer Count set bits in an integer link. W
2 min read
Python | Count the elements in a list until an element is a Tuple
In this problem, we need to accept a list. The list can have nested tuples. We need to count the elements in a list until a tuple has been encountered. Examples: Input : [4, 5, 6, 10, (1, 2, 3), 11, 2, 4] Output : 4 Input : [4, (5, 6), 10, (1, 2, 3), 11, 2, 4] Output : 1 Method #1: In this program we will use the concept of isinstance() to verify w
2 min read
Python | Integer count in Mixed List
The lists in python can handle different type of data types in it. The manipulation of such lists is complicated. Sometimes we have a problem in which we need to find the count of integer values in which the list can contain string as a data type i.e heterogeneous. Let’s discuss certain ways in which this can be performed.Method #1 : Using list com
6 min read
Python program to count Even and Odd numbers in a List
Given a list of numbers, write a Python program to count Even and Odd numbers in a List. Example: Input: list1 = [2, 7, 5, 64, 14]Output: Even = 3, odd = 2 Input: list2 = [12, 14, 95, 3]Output: Even = 2, odd = 2 Example 1: Count Even and Odd numbers from the given list using for loop Iterate each element in the list using for loop and check if num
9 min read
Python program to count positive and negative numbers in a list
Given a list of numbers, write a Python program to count positive and negative numbers in a List. Example: Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 1 Example #1: Count positive and negative numbers from the given list using for loop Iterate each element in the list using for
6 min read
three90RightbarBannerImg