Last modified: Apr 24, 2026 By Alexander Williams
Fix TypeError: List Not Integer
This error is common for Python beginners. It happens when you use a list where Python expects a single integer. The message is clear: you gave a list, but the function needed a number.
Understanding this error is key to writing clean code. It often occurs in loops, indexing, or built-in functions. Let's break down why it happens and how to fix it.
What Does This Error Mean?
Python is a strongly typed language. Some functions and operations require an integer argument. When you pass a list instead, Python cannot interpret it as a number. It raises a TypeError.
For example, the range() function expects an integer. If you give it a list, you get this error. The same applies to indexing and other operations.
Common Causes of This Error
There are three main reasons you might see this error. Each has a simple fix.
1. Using a List with range()
The range() function creates a sequence of numbers. It needs an integer start, stop, or step. If you pass a list, Python cannot convert it.
# Wrong: passing a list to range()
my_list = [1, 2, 3]
for i in range(my_list): # Error here
print(i)
TypeError: 'list' object cannot be interpreted as an integer
To fix this, use len() on the list. This returns the number of elements as an integer.
# Correct: use len() to get the integer length
my_list = [1, 2, 3]
for i in range(len(my_list)): # len() returns 3
print(my_list[i])
1
2
3
2. Confusing Indexing with Slicing
List indexing uses a single integer inside square brackets. Slicing uses a colon and two integers. If you try to use a list as an index, Python throws this error.
# Wrong: using a list as an index
data = [10, 20, 30]
index = [1, 2] # This is a list, not an integer
print(data[index]) # Error
TypeError: list indices must be integers or slices, not list
The error message is slightly different but related. Always use an integer for a single index. Use a slice for a range.
# Correct: use integer for index, slice for range
data = [10, 20, 30]
print(data[1]) # Integer index
print(data[1:3]) # Slice with colon
20
[20, 30]
3. Passing a List to Functions That Expect Integers
Many Python functions like print() work with lists, but others don't. Functions such as chr(), ord(), and abs() need an integer. If you pass a list, you get the error.
# Wrong: chr() expects an integer
codes = [65, 66]
print(chr(codes)) # Error
TypeError: 'list' object cannot be interpreted as an integer
Fix this by iterating over the list and passing each integer one at a time.
# Correct: iterate and pass each integer
codes = [65, 66]
for code in codes:
print(chr(code))
A
B
How to Debug This Error
When you see this error, check the line number in the traceback. Look for where you passed a list instead of an integer. Use print(type(your_variable)) to see the data type.
If you are working with loops, remember that range() needs an integer. Use len() to get the length. For more complex issues, review your code logic. Sometimes you accidentally assign a list to a variable that should hold an integer.
For a deeper dive into common Python errors, read our guide on Python TypeError: Causes and Fixes. It covers many similar issues and their solutions.
Real-World Example: Processing User Input
Imagine you ask a user for a number of items. If the input is a list, you get this error. Always convert user input to the correct type.
# User input might be a string, but if it's a list, error occurs
user_input = ["5"] # This is a list of strings
try:
num_items = int(user_input) # Error: cannot convert list to int
except TypeError:
print("Please enter a single number, not a list.")
Please enter a single number, not a list.
Always validate your data. Use isinstance() to check if a variable is an integer before using it in functions like range().
Best Practices to Avoid This Error
Follow these tips to prevent the error in your code.
- Always use
len()when you need the size of a list. - Use
isinstance(variable, int)to check types before operations. - When iterating, use
for item in list:instead of indexing withrange(). - Read function documentation to know what arguments they expect.
- Test your code with small examples to catch errors early.
Conclusion
The TypeError: 'list' object cannot be interpreted as an integer is easy to fix once you understand the cause. Always check the data type you are passing to functions. Use len() for list sizes, iterate properly, and validate inputs.
Remember, Python gives clear error messages. Read the traceback carefully. With practice, you will avoid this error and write more robust code. For further reading, explore our article on Python TypeError: Causes and Fixes to master other common errors.
Keep coding and testing. Every error is a learning opportunity.