python count item frequency in a list banner

Python List Count Item Frequency

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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top