Python List count() Method
list.count() tells you how many times one value appears in a list.
It is a useful method when you want to:
- count one specific item
- check whether a value appears more than once
- quickly see how many matches exist
It returns a number and does not change the original list.
Quick answer #
numbers = [1, 2, 2, 3, 2]
result = numbers.count(2)
print(result) # 3
Use list.count(value) to count how many times one value appears in a list.
What list.count() does #
list.count():
- counts how many times a given value appears in a list
- returns an integer
- does not change the original list
- checks for exact matches
This means Python looks through the whole list and compares each item to the value you gave it.
If the value appears 3 times, the method returns 3.
If it does not appear at all, the method returns 0.
Basic syntax #
The syntax is:
my_list.count(value)
my_listis the list you want to searchvalueis the item you want to count
Example:
letters = ["a", "b", "a", "c"]
print(letters.count("a"))
Output:
2
Python checks the whole list. If the value is not found, it returns 0.
What the return value means #
The return value from count() is always an int.
Possible results:
0if the value does not exist in the list1if it appears once2or more if it appears multiple times
Example:
colors = ["red", "blue", "green", "blue"]
print(colors.count("blue")) # 2
print(colors.count("red")) # 1
print(colors.count("yellow")) # 0
If you only want to know whether a value exists, count() works, but using in is often simpler. See how to check if a value exists in a list in Python.
How matching works #
list.count() uses equality matching.
That means Python checks whether each item is equal to the value you passed in.
Important details:
- matches are based on equality
- strings are case-sensitive
1andTruemay compare as equal in Python- nested lists can be counted if the entire nested list matches
Strings are case-sensitive #
words = ["apple", "Apple", "apple"]
print(words.count("apple")) # 2
print(words.count("Apple")) # 1
"apple" and "Apple" are different strings.
1 and True can match #
values = [1, True, 2, 1]
print(values.count(1)) # 3
print(values.count(True)) # 3
This surprises many beginners.
In Python:
True == 1False == 0
So count() may include both booleans and integers when they compare as equal.
Nested lists must match exactly #
items = [[1, 2], [3, 4], [1, 2], [1]]
print(items.count([1, 2])) # 2
Python counts only exact matches of the full nested list.
Beginner examples to include #
Count a number in a list of numbers #
numbers = [10, 20, 10, 30, 10]
result = numbers.count(10)
print(result)
Output:
3
Count a word in a list of strings #
words = ["cat", "dog", "cat", "bird"]
result = words.count("cat")
print(result)
Output:
2
Count a value that is not in the list #
numbers = [1, 2, 3, 4]
result = numbers.count(5)
print(result)
Output:
0
Count repeated nested list values #
data = [[1, 2], [1, 2], [2, 3], [1, 2]]
result = data.count([1, 2])
print(result)
Output:
3
When to use count() #
Use count() when you need the number of appearances of one value.
Good use cases:
- counting one specific item
- checking whether a list contains duplicates of a value
- doing a quick count before making a decision
Example:
votes = ["yes", "no", "yes", "yes"]
yes_votes = votes.count("yes")
print(yes_votes)
count() is not the best choice if you need counts for many different values. In that case, you would usually use a loop or another tool.
If you are still learning list basics, see Python lists explained for beginners and what is a list in Python.
Common mistakes #
Here are some common beginner mistakes with list.count().
Trying to count part of a string instead of list items #
list.count() counts list items, not parts of a string.
words = ["apple pie", "banana", "apple pie"]
print(words.count("apple")) # 0
Why 0?
Because "apple" is not a full item in the list. The list contains "apple pie".
If your data is a string instead of a list, you may be thinking of str.count().
Expecting count() to return True or False #
count() returns a number, not a boolean.
numbers = [1, 2, 2, 3]
print(numbers.count(2)) # 2
If you want a true/false answer, use:
print(2 in numbers) # True
Forgetting that string matching is case-sensitive #
names = ["Sam", "sam", "SAM"]
print(names.count("sam")) # 1
Only the exact string "sam" matches.
Calling count() on the wrong type #
If you are not sure what your variable contains, inspect it first:
print(my_list)
print(type(my_list))
print(my_list.count(value))
print(value in my_list)
for item in my_list:
print(repr(item))
These checks can help you see:
- whether the variable is really a list
- whether the values are exactly what you expect
- whether spacing, capitalization, or data type differences are causing problems
Common causes of confusion include:
- using
count()when the data is a string, not a list - expecting substring behavior instead of exact list-item matching
- confusing
list.count()with stringcount() - being surprised that booleans and integers can compare as equal
FAQ #
Does list.count() change the list? #
No. It only returns how many times a value appears.
What happens if the value is not in the list? #
The method returns 0.
Can list.count() count multiple different values at once? #
No. It counts one value per call.
Is list.count() case-sensitive for strings? #
Yes. "Apple" and "apple" are different values.
Why does True sometimes count like 1? #
In Python, True compares equal to 1 and False compares equal to 0.