The python list function count() is used to count the number of times an item appears in a list.
Lists in python are an ordered collection of objects and it may happen that you require the number of times a particular element occurs in a list. For example, you have a list of all Wimbledon Winners for the last 20 years and you want to find the number of times Roger Federer won the title. In such a case, the count()
function is quite useful. In this tutorial, we’ll cover how to use it, its syntax, and some examples.
The count() function
As mentioned above, count()
is a list function used to count the frequency of a given item in a list.
Syntax:
sample_list.count(x)
Here, sample_list is the list in which you want to count the frequency of the element x.
Parameters:
- x: The element to be counted.
Returns:
The count()
function returns the number of times the element appears in the list.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Examples
Example 1: Counting the number of Cricket WorldCups won by teams after 2000.
# list of world cup winners from 2000 to 2020 in ODI cricket
cricket_wc_winners = ['Australia', 'Australia', 'India', 'Australia', 'England']
# print the heading
print("Cricket ODI World Cup wins from 2000 to 2020")
# WC wins by Australia
aus_count = cricket_wc_winners.count('Australia')
# print WC wins by Australia
print("Australia:", aus_count)
# WC wins by India
ind_count = cricket_wc_winners.count('India')
# print WC wins by India
print("India:", ind_count)
# WC wins by England
eng_count = cricket_wc_winners.count('England')
# print WC wins by England
print("England:", eng_count)
Output:
Cricket ODI World Cup wins from 2000 to 2020
Australia: 3
India: 1
England: 1
In the above example, the list cricket_wc_winners
stores the winners of the Cricket World Cup from 2000 to 2020. The python list count() function is used to count the number of times different countries won the world cup. For instance, the element 'Australia'
appears three times in the list which is the same as the frequency returned by the count()
function.
Example 2: Count of an element not present in the list
# list of world cup winners from 2000 to 2020 in ODI cricket
cricket_wc_winners = ['Australia', 'Australia', 'India', 'Australia', 'England']
# print the heading
print("Cricket ODI World Cup wins from 2000 to 2020")
# WC wins by South Africa
sa_count = cricket_wc_winners.count('South Africa')
# print WC wins by South Africa
print("South Africa:", sa_count)
Output:
Cricket ODI World Cup wins from 2000 to 2020
South Africa: 0
In the above example, the count()
function is used to count the frequency of the item 'South Africa'
in the list. It returns 0
as the element is not present in the list.
Example 3: Count of complex objects like list or tuple inside the list
# list
ls = ['A', 'B', 'C', 1, 2, 3, ('A', 1), ('B', 2), ('C', 3), ('A', 1)]
# count of 'A'
A_count = ls.count('A')
# print the count of 'A'
print("Count of 'A':", A_count)
# count of 1
one_count = ls.count(1)
# print the count of 1
print("Count of 1:", one_count)
# count of ('A', 1)
A1_count = ls.count(('A', 1))
# print the count of ('A', 1)
print("Count of ('A', 1):", A1_count)
Output:
Count of 'A': 1
Count of 1: 1
Count of ('A', 1): 2
In the above example, the list ls
contains a mix of elements like strings, integers, and even tuples. The count()
function when used to count the tuple ('A', 1)
returns 2
since this tuple is present twice in the list. The count function behaves similarly with tuples or other complex items as it does with strings or integers.
For more on python list functions refer to the python docs.
For more lists and similar data structures, check out our guide on lists and other data structures in python.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.