Last modified: May 25, 2026
Python List Count: Easy Guide
The count() method in Python is simple. It counts how many times a specific item appears in a list. This is a common task when working with data. You might need to find duplicates or check frequency.
This guide will teach you everything about list.count(). You will learn the syntax, see examples, and understand common errors. We will keep it clean and easy to follow.
What is the Python List Count Method?
The count() method returns an integer. This integer is the number of times a given element appears in the list. It is a built-in method. This means you can use it on any list object.
Important: The method only counts exact matches. It is case-sensitive for strings. It also respects data types. For example, the integer 1 is different from the string "1".
Syntax of List Count
The syntax is very direct. You write the list name, then a dot, then the method. Inside the parentheses, you put the item you want to count.
# Basic syntax
list_name.count(element)
The element is the value you are searching for. The method will scan the entire list. It will return the total count.
Basic Example of List Count
Let's start with a simple example. We have a list of fruits. We want to know how many times "apple" appears.
# Define a list of fruits
fruits = ["apple", "banana", "apple", "orange", "apple", "grape"]
# Count how many times 'apple' appears
apple_count = fruits.count("apple")
# Print the result
print(apple_count) # Output: 3
3
As you can see, "apple" appears three times. The method counted each occurrence correctly.
Counting Numbers in a List
You can also count numbers. The method works the same way for integers and floats.
# List of numbers
numbers = [1, 2, 3, 1, 4, 1, 5, 2]
# Count the number 1
count_one = numbers.count(1)
print(count_one) # Output: 3
# Count the number 2
count_two = numbers.count(2)
print(count_two) # Output: 2
3
2
This is useful for analyzing numerical data. You can quickly find the frequency of a value.
Counting with Mixed Data Types
Python lists can hold different data types. The count() method handles this well. Remember, it checks for exact matches.
# Mixed list
mixed = [1, "1", 1.0, "hello", 1, "world"]
# Count the integer 1
count_int = mixed.count(1)
print(count_int) # Output: 2
# Count the string "1"
count_str = mixed.count("1")
print(count_str) # Output: 1
2
1
Note: The integer 1 and the string "1" are not the same. The float 1.0 is equal to the integer 1 in Python. So it is counted together with the integer.
Using Count in Conditional Statements
You can use count() inside an if statement. This helps you check if an item appears a certain number of times. For a deeper look into list length checks, see our guide on Python List Length Check with If.
# List of scores
scores = [85, 90, 85, 70, 85, 95]
# Check if 85 appears more than 2 times
if scores.count(85) > 2:
print("85 appears more than twice!")
else:
print("85 appears two times or less.")
85 appears more than twice!
This pattern is common for data validation. You can ensure no item appears too many times.
Counting with Nested Lists
The count() method works on the top level only. It does not look inside nested lists. It counts the entire sublist as one item.
# List with nested lists
nested = [1, [2, 3], [2, 3], 4]
# Count the sublist [2, 3]
count_sublist = nested.count([2, 3])
print(count_sublist) # Output: 2
# Count the integer 1
count_one = nested.count(1)
print(count_one) # Output: 1
2
1
If you need to count items inside nested lists, you need a different approach. You can use loops or list comprehension.
What Happens if the Item is Not Found?
If the item you search for does not exist, count() returns 0. It does not raise an error. This makes it safe to use.
# List of colors
colors = ["red", "blue", "green"]
# Count a color not in the list
count_yellow = colors.count("yellow")
print(count_yellow) # Output: 0
0
This is very helpful. You can check if an item exists without worrying about exceptions.
Performance of List Count
The count() method has a time complexity of O(n). This means it scans the entire list. For large lists, this can be slow. If you need to count many different items, consider using a Counter from the collections module. For more on list operation performance, you can read about Python List Insert Time Complexity.
from collections import Counter
# Large list
data = [1, 2, 3, 1, 2, 1, 1, 2, 3, 3, 3]
# Use Counter for multiple counts
counter = Counter(data)
print(counter[1]) # Output: 4
print(counter[2]) # Output: 3
print(counter[3]) # Output: 4
4
3
4
For a single count, list.count() is fine. For many counts, Counter is more efficient.
Common Mistakes with List Count
Beginners often make small mistakes. Here are the most common ones.
Mistake 1: Forgetting that the method is case-sensitive.
words = ["Hello", "hello", "HELLO"]
print(words.count("hello")) # Output: 1
1
Only the exact match "hello" is counted. To count case-insensitively, convert all items to lowercase first.
Mistake 2: Trying to count items in a tuple or string directly on the list. The method only works on lists. For strings, use str.count().
Practical Example: Removing Duplicates
You can combine count() with a loop to remove duplicates. This is a common interview question.
# List with duplicates
original = [1, 2, 2, 3, 4, 4, 4, 5]
# Create a new list without duplicates
unique = []
for item in original:
if original.count(item) == 1:
unique.append(item)
elif item not in unique:
unique.append(item)
print(unique) # Output: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
This method is simple but not the most efficient for large lists. It works well for learning purposes.
Combining Count with Other List Methods
You can use count() with other methods like insert() or extend(). For example, you might want to insert an item only if it appears a certain number of times. To learn more about inserting items, check our guide on Python List Insert Syntax Guide.
# List of tasks
tasks = ["write", "read", "write", "test"]
# Insert a new task only if "write" appears exactly 2 times
if tasks.count("write") == 2:
tasks.insert(0, "review")
print(tasks) # Output: ['review', 'write', 'read', 'write', 'test']
['review', 'write', 'read', 'write', 'test']
This shows how count() can be part of a larger logic flow.
Conclusion
The list.count() method is a fundamental tool in Python. It is easy to use and very readable. You now know how to count items in a list, handle different data types, and avoid common mistakes.
Remember that count() is case-sensitive and only works on the top level. For large datasets, consider using Counter. Practice with your own lists to get comfortable.
This method is perfect for beginners. It helps you understand how Python handles data. Use it in your next project to analyze list frequencies.