In this tutorial, I will explain how to count occurrences of elements in a Python array. One of my team members asked me about counting occurrences in a Python array. Then I thought of publishing this article. I explain various methods to achieve this, Let us understand more about the topic.
Python offers several methods to count occurrences in an array (or list). We’ll explore the most effective ones, including the count() method, collections.Counter and list comprehensions.
Check out setting an array element with a sequence error in Python
1. Using the count() Method
The simplest way to count occurrences of an element in the list is by using the count() method. This method is very easy to implement.
Let us consider an example of US states to count the number of occurrences:
# Example: Counting occurrences of states in a survey
states = ["California", "Texas", "New York", "California", "Texas", "California"]
# Count occurrences of "California"
california_count = states.count("California")
print(f"California appears {california_count} times in the list.")Output:
California appears 3 times in the list.Here is the screenshot of the executed example code.

In this example, the count() method returns the number of times “California” appears in the list.
How to Sort an Array in Python
2. Using collections.Counter
For more complex tasks, collections.Counter from the collections is useful. It not only returns the number of occurrences it is also stored in the form of a dictionary.
from collections import Counter
# Example: Counting occurrences of states in a survey
states = ["California", "Texas", "New York", "California", "Texas", "California"]
# Use Counter to count occurrences
state_counts = Counter(states)
print(state_counts)
# Access count for a specific state
print(f"California appears {state_counts['California']} times in the list.")Output:
Counter({'California': 3, 'Texas': 2, 'New York': 1})
California appears 3 times in the list.Here is the screenshot of the executed example code.

With Counter, you get the count of all elements in the list, making it easier to analyze data.
Check out Python program to print the smallest element in an array
3. Using List Comprehensions
List comprehensions is an easy approach, and can be useful for specific scenarios. They allow you to filter and count elements in a single line of code.
# Example: Counting occurrences of states in a survey
states = ["California", "Texas", "New York", "California", "Texas", "California"]
# Count occurrences using list comprehension
california_count = sum(1 for state in states if state == "California")
print(f"California appears {california_count} times in the list.")Output:
California appears 3 times in the list.Here is the screenshot of the executed example code.

This method is less efficient for large datasets but provides a clear way to count occurrences.
Read How to Split a String into an Array in Python
Example
Let us consider a real-world example where we analyze survey data from different states in the USA. We will use the Counter() method to make it easy.
from collections import Counter
# Survey data from various states
survey_data = [
"California", "Texas", "New York", "California", "Texas", "California",
"Florida", "New York", "Florida", "Texas", "California", "Florida",
"New York", "Texas", "Florida", "California", "Texas", "New York"
]
# Count occurrences of each state
state_counts = Counter(survey_data)
# Display the results
for state, count in state_counts.items():
print(f"{state}: {count} responses")Output:
California: 5 responses
Texas: 5 responses
New York: 4 responses
Florida: 4 responsesBy using Counter(), we can easily count how many responses we received from each state.
Check out 3D Arrays in Python
Handling Large Datasets
When dealing with large datasets, performance is very important. While the count() method list comprehensions are simple, they are not most efficient for large lists. collections.Counter is generally more efficient and should be preferred for larger datasets.
Here’s a performance comparison using a larger dataset:
import time
from collections import Counter
# Generate a large list of states
large_survey_data = ["California"] * 1000000 + ["Texas"] * 500000 + ["New York"] * 250000
# Using count() method
start_time = time.time()
california_count = large_survey_data.count("California")
end_time = time.time()
print(f"Count method: California appears {california_count} times. Time taken: {end_time - start_time} seconds")
# Using Counter
start_time = time.time()
state_counts = Counter(large_survey_data)
end_time = time.time()
print(f"Counter method: California appears {state_counts['California']} times. Time taken: {end_time - start_time} seconds")In this example, the Counter() method typically outperforms the count() method, especially with larger datasets.
Read How to Get Values from a JSON Array in Python
Conclusion
In this tutorial, I covered various methods to count occurrences in a Python array, by using the count() method, I also discussed collections.Counter and by using List Comprehensions, and real world examples, I explained how to deal with when there is large Datasets.
Hope this tutorial helped you to learn more about counting occurrences in a Python array. Happy coding..!
You may also like to read:
- How to Use Python Array Index -1
- How to Save an Array to a File in Python
- How to Convert Array to Set 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.