In this tutorial, I will explain how to count the frequency of elements in a Python list. As a Python developer working on various projects for my clients, I came across a scenario where I needed to count the frequency of elements in a list. Then, I researched more about this topic and found a few efficient methods to accomplish this task, and I will share my findings with suitable examples.
Count the Frequency of Elements in a Python List
Let’s say you’re working on a project that involves analyzing a list of names of US presidents. You have a list containing the last names of all the presidents, and you want to count how many times each name appears in the list. This is where counting frequencies comes into play.
For example, let’s consider the following list of US presidents’ last names:
presidents = ['Washington', 'Adams', 'Jefferson', 'Madison', 'Monroe', 'Adams', 'Jackson', 'Van Buren', 'Harrison', 'Tyler', 'Polk', 'Taylor', 'Fillmore', 'Pierce', 'Buchanan', 'Lincoln', 'Johnson', 'Grant', 'Hayes', 'Garfield', 'Arthur', 'Cleveland', 'Harrison', 'Cleveland', 'McKinley', 'Roosevelt', 'Taft', 'Wilson', 'Harding', 'Coolidge', 'Hoover', 'Roosevelt', 'Truman', 'Eisenhower', 'Kennedy', 'Johnson', 'Nixon', 'Ford', 'Carter', 'Reagan', 'Bush', 'Clinton', 'Bush', 'Obama', 'Trump', 'Biden']We aim to count how many times each last name appears in the list.
Read How to Initialize a List of Size N in Python?
Method 1: Use the count() Function
Python provides a built-in count() function that allows you to count the occurrences of a specific element in a list. Here’s how you can use it:
name = 'Adams'
frequency = presidents.count(name)
print(f"The name '{name}' appears {frequency} times in the list.")Output:
The name 'Adams' appears 2 times in the list.You can see the output in the screenshot below.

The count() function is straightforward to use. You simply pass the element you want to count as an argument, and it returns the number of occurrences of that element in the list.
Example: Count Frequencies of Multiple Names
Let’s count the frequencies of multiple names in the list:
names_to_count = ['Roosevelt', 'Adams', 'Bush', 'Clinton']
for name in names_to_count:
frequency = presidents.count(name)
print(f"The name '{name}' appears {frequency} times in the list.")Output:
The name 'Roosevelt' appears 2 times in the list.
The name 'Adams' appears 2 times in the list.
The name 'Bush' appears 2 times in the list.
The name 'Clinton' appears 1 times in the list.Check out How to Get the Index of an Element in a List in Python?
Method 2: Use a Dictionary
Another way to count frequencies is by using a Python dictionary. A dictionary allows you to store key-value pairs, where the key is the element (in this case, the president’s last name) and the value is the frequency count.
Here’s an example of counting frequencies using a dictionary:
frequency_dict = {}
for name in presidents:
if name in frequency_dict:
frequency_dict[name] += 1
else:
frequency_dict[name] = 1
print(frequency_dict)Output:
{'Washington': 1, 'Adams': 2, 'Jefferson': 1, 'Madison': 1, 'Monroe': 1, 'Jackson': 1, 'Van Buren': 1, 'Harrison': 2, 'Tyler': 1, 'Polk': 1, 'Taylor': 1, 'Fillmore': 1, 'Pierce': 1, 'Buchanan': 1, 'Lincoln': 1, 'Johnson': 2, 'Grant': 1, 'Hayes': 1, 'Garfield': 1, 'Arthur': 1, 'Cleveland': 2, 'McKinley': 1, 'Roosevelt': 2, 'Taft': 1, 'Wilson': 1, 'Harding': 1, 'Coolidge': 1, 'Hoover': 1, 'Truman': 1, 'Eisenhower': 1, 'Kennedy': 1, 'Nixon': 1, 'Ford': 1, 'Carter': 1, 'Reagan': 1, 'Bush': 2, 'Clinton': 1, 'Obama': 1, 'Trump': 1, 'Biden': 1}You can see the output in the screenshot below.

In this approach, we iterate over each name in the presidents list. If the name already exists as a key in the, frequency_dict we increment its corresponding value by 1. If the name doesn’t exist, we add it to the dictionary with a value of 1.
Retrieve Frequencies from the Dictionary
Once you have the frequency dictionary, you can easily retrieve the frequency of a specific name:
name = 'Roosevelt'
frequency = frequency_dict.get(name, 0)
print(f"The name '{name}' appears {frequency} times in the list.")Output:
The name 'Roosevelt' appears 2 times in the list.The get() method allows you to retrieve the value associated with a key. If the key doesn’t exist, it returns a default value (in this case, 0).
Read How to Merge Lists Without Duplicates in Python?
Method 3: Use the Counter Class
Python’s collections module provides a convenient class called Counter that is specifically designed for counting frequencies of elements in a list or any iterable.
Here’s an example of using the Counter class:
from collections import Counter
frequency_counter = Counter(presidents)
print(frequency_counter)Output:
Counter({'Adams': 2, 'Harrison': 2, 'Johnson': 2, 'Cleveland': 2, 'Roosevelt': 2, 'Bush': 2, 'Washington': 1, 'Jefferson': 1, 'Madison': 1, 'Monroe': 1, 'Jackson': 1, 'Van Buren': 1, 'Tyler': 1, 'Polk': 1, 'Taylor': 1, 'Fillmore': 1, 'Pierce': 1, 'Buchanan': 1, 'Lincoln': 1, 'Grant': 1, 'Hayes': 1, 'Garfield': 1, 'Arthur': 1, 'McKinley': 1, 'Taft': 1, 'Wilson': 1, 'Harding': 1, 'Coolidge': 1, 'Hoover': 1, 'Truman': 1, 'Eisenhower': 1, 'Kennedy': 1, 'Nixon': 1, 'Ford': 1, 'Carter': 1, 'Reagan': 1, 'Clinton': 1, 'Obama': 1, 'Trump': 1, 'Biden': 1})You can see the output in the screenshot below.

The Counter class automatically counts the frequencies of each element in the list and returns a dictionary-like object where the keys are the elements and the values are their frequencies.
Retrieving Frequencies from the Counter
You can retrieve the frequency of a specific name from the Counter object using the same syntax as a dictionary:
name = 'Kennedy'
frequency = frequency_counter[name]
print(f"The name '{name}' appears {frequency} times in the list.")Output:
The name 'Kennedy' appears 1 times in the list.The Counter class provides additional methods and functionality for working with frequencies, such as retrieving the most common elements, updating counts, and more.
Check out How to Convert String to List in Python?
Sort the Results
After counting the frequencies, you may want to sort the results based on the frequency counts. You can achieve this using the sorted() function in Python.
Example: Sort Frequency Dictionary
sorted_frequency = sorted(frequency_dict.items(), key=lambda x: x[1], reverse=True)
print(sorted_frequency)Output:
[('Adams', 2), ('Harrison', 2), ('Johnson', 2), ('Cleveland', 2), ('Roosevelt', 2), ('Bush', 2), ('Washington', 1), ('Jefferson', 1), ('Madison', 1), ('Monroe', 1), ('Jackson', 1), ('Van Buren', 1), ('Tyler', 1), ('Polk', 1), ('Taylor', 1), ('Fillmore', 1), ('Pierce', 1), ('Buchanan', 1), ('Lincoln', 1), ('Grant', 1), ('Hayes', 1), ('Garfield', 1), ('Arthur', 1), ('McKinley', 1), ('Taft', 1), ('Wilson', 1), ('Harding', 1), ('Coolidge', 1), ('Hoover', 1), ('Truman', 1), ('Eisenhower', 1), ('Kennedy', 1), ('Nixon', 1), ('Ford', 1), ('Carter', 1), ('Reagan', 1), ('Clinton', 1), ('Obama', 1), ('Trump', 1), ('Biden', 1)]In this example, we use sorted() to sort the items of the frequency_dict based on the frequency counts (the second element of each item tuple). We specify reverse=True to sort in descending order.
Check out Convert String to List in Python Without Using Split
Example: Sort Counter Results
sorted_frequency = frequency_counter.most_common()
print(sorted_frequency)Output:
[('Adams', 2), ('Harrison', 2), ('Johnson', 2), ('Cleveland', 2), ('Roosevelt', 2), ('Bush', 2), ('Washington', 1), ('Jefferson', 1), ('Madison', 1), ('Monroe', 1), ('Jackson', 1), ('Van Buren', 1), ('Tyler', 1), ('Polk', 1), ('Taylor', 1), ('Fillmore', 1), ('Pierce', 1), ('Buchanan', 1), ('Lincoln', 1), ('Grant', 1), ('Hayes', 1), ('Garfield', 1), ('Arthur', 1), ('McKinley', 1), ('Taft', 1), ('Wilson', 1), ('Harding', 1), ('Coolidge', 1), ('Hoover', 1), ('Truman', 1), ('Eisenhower', 1), ('Kennedy', 1), ('Nixon', 1), ('Ford', 1), ('Carter', 1), ('Reagan', 1), ('Clinton', 1), ('Obama', 1), ('Trump', 1), ('Biden', 1)]The Counter class provides the most_common() method, which returns a list of the elements and their frequencies, sorted in descending order by frequency.
Conclusion
In this tutorial, I explained how to count the frequency of elements in a Python list. I discussed their methods, such as using the count() function, using a dictionary and using the counter class. I also explained how to sort the results with two examples.
You may like to read:
- How to Iterate Through a List in Python?
- How to Write a List to a File in Python?
- How to Select Items from a List in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.