Last modified: Dec 19, 2025 By Alexander Williams
Fix Python AttributeError 'list' No 'count'
Python errors can be frustrating. The AttributeError is a common one.
This error means you tried to use a method on an object that doesn't have it.
Specifically, 'list' object has no attribute 'count' is confusing. Lists do have a count() method.
The error often means you are not calling count() on a list. You are calling it on something else.
Understanding the .count() Method
The count() method is built into Python lists.
It returns the number of times a specified value appears in the list.
Its syntax is simple: list_name.count(value).
Here is a correct example of using list.count().
# Correct usage of the list.count() method
my_list = [1, 2, 3, 2, 4, 2, 5]
count_of_twos = my_list.count(2)
print(f"The number 2 appears {count_of_twos} times.")
The number 2 appears 3 times.
Why the Error Occurs
The error occurs when Python cannot find a count attribute.
This happens if the object you're calling .count() on is not a list.
You might have a variable that you think is a list. But it might be a different type.
Common causes include nested lists, variable reassignment, or string operations.
Cause 1: Calling .count() on a Non-List
The most frequent cause is simple. You are not using a list object.
You might be trying to count characters in a string incorrectly.
Or you might be dealing with an integer, tuple, or dictionary.
Only list objects have the .count() method for counting elements.
For example, strings have their own count() method for substrings.
If you get this error on a string, see our guide on Fix AttributeError: 'str' object has no attribute 'count'.
# ERROR: Calling .count() on an integer
my_var = 42
# This will raise AttributeError: 'int' object has no attribute 'count'
# result = my_var.count(2)
Cause 2: Nested List Confusion
You might have a list of lists. You try to call .count() on an inner list.
But you accidentally call it on the outer list with the wrong syntax.
This mistake is easy to make when working with complex data structures.
# A nested list example
nested_list = [[1, 2], [3, 4], [1, 2]]
# GOAL: Count how many times the inner list [1,2] appears.
# WRONG: This tries to call .count() on the outer list with an invalid argument.
# count = nested_list.count(1, 2) # SyntaxError & conceptual error
# CORRECT: Call .count() on the outer list with the exact inner list as argument.
count = nested_list.count([1, 2])
print(f"The list [1,2] appears {count} times.")
The list [1,2] appears 2 times.
Cause 3: Variable Reassignment or Shadowing
You may have reassigned your list variable. It now holds a different data type.
Or a built-in function like list might have been overwritten.
This is a common pitfall in longer scripts or Jupyter notebooks.
# Example of variable reassignment causing the error
my_data = [10, 20, 30]
# Later in the code, accidentally reassign the variable
my_data = "This is a string now"
# Now calling .count() will fail because my_data is a string, not a list.
# The string .count() method exists, but the error message might be misleading
# if the original intention was to use list.count().
print(type(my_data)) # Check the type
Step-by-Step Debugging Guide
Follow these steps to find and fix the error in your code.
Step 1: Check the Object Type
Use the type() function. Confirm the variable is a list.
Print the type right before the line causing the error.
This will show you what the object really is.
problem_var = get_data_from_somewhere() # Example source
print(f"Type of problem_var: {type(problem_var)}")
print(f"Value of problem_var: {problem_var}")
# Now try the count operation
# If it's a list, this will work.
Step 2: Verify Method Existence with dir()
Use the dir() function. It lists all attributes of an object.
Look for 'count' in the output. If it's not there, you have the wrong type.
my_object = [1, 2, 3]
print(dir(my_object)) # Look for 'count' in the long list
# A quicker check:
print('count' in dir(my_object)) # Should print True for a list
Step 3: Review Variable Assignment
Trace back through your code. Look where the variable was created.
See if it was reassigned later. Check function returns.
Ensure a function that should return a list actually does.
Common Solutions and Examples
Solution 1: Ensure You Have a List
Convert your data to a list if needed. Use the list() constructor.
This works if you have a tuple, set, or string of characters.
# You have a tuple but need a list
my_tuple = (1, 2, 2, 3)
my_list = list(my_tuple) # Convert tuple to list
count = my_list.count(2)
print(count)
2
Solution 2: Correct Your Indexing
If working with nested lists, ensure you are indexing correctly.
You must call .count() on the specific list you want to search.
data = [[1, 1, 2], [3, 4], [1, 1, 2]]
# To count '1' in the first inner list:
first_inner_list = data[0] # This is [1, 1, 2]
count_of_ones = first_inner_list.count(1) # Call .count() on the list
print(f"Number of 1s in first list: {count_of_ones}")
Number of 1s in first list: 2
Solution 3: Avoid Built-in Name Shadowing
Never use list, str, or dict as variable names.
If you do, you overwrite the built-in function. This causes strange errors.
If you have, rename your variable and restart your Python session.
# BAD: Shadows the built-in list()
list = [1, 2, 3]
# Now the name 'list' refers to your list, not the built-in class.
# This can cause errors elsewhere.
# GOOD: Use a descriptive name
my_numbers = [1, 2, 3]
Related AttributeErrors
AttributeErrors are common with other methods and types.
For example, trying to use clear() on an integer causes an error.
Learn more about fixing Fix Python AttributeError 'int' No 'clear'.
Similarly, dictionaries have specific methods. Errors occur if misapplied.
See our guide on Fix Python AttributeError 'dict' No 'clear' for details.
Conclusion
The AttributeError 'list' object has no attribute 'count' is a type error.
You are calling the .count() method on something that is not a list.
Use type() and dir() to debug. Check for variable reassignment.
Ensure you are working with a genuine list object. Convert data if necessary.
Understanding object types is key to avoiding this and similar errors in Python.
Always verify your data structures before calling methods on them.