Python TypeError List Object Cannot be Interpreted as an Integer [Fixed]
Written by Kolade Chris | Jan 11, 2025 | #Error #Python | 2 minutes Read
In Python, using a list directly in a built-in function that expects an integer input will result in the error `TypeError: ‘list’ object cannot be interpreted as an integer.
Let’s look at a few causes of the error and how you can fix each of them.
What Causes the List Object Cannot be Interpreted as an Integer Error?
A good example of what causes the error is using a list in the range()
function:
If you also use a list as the step parameter of the range()
function, the error will be triggered:
Another built-in function that expects an integer is round()
. SO, using a list in it will trigger the same error:
enumerate
is another function that expects an integer as its optional start
parameter. If you go ahead and use a list for that, you’ll get the error:
How to Fix the List Object Cannot be Interpreted as an Integer Error
If you’re working with the range()
function and its causing the TypeError: 'list' object cannot be interpreted as an integer
error, use the length of the list instead by wrapping it in the len()
function:
Using len()
on the list will return the length of the list, which is a valid integer.
Since three items are in the my_list
list above, that integer would be 3
, hence the 0 1 2
result.
If using the enumerate()
in a code causes the error, find a way to make sure the start parameter is accepting an integer, not a list.
This is incorrect:
This is correct:
This is also correct:
I hope this article helped you fix the error. Thank you for reading!