Last updated: Apr 8, 2024
Reading timeยท8 min
The Python "TypeError: 'list' object cannot be interpreted as an integer"
occurs when we pass a list to a function that expects an integer argument, e.g.
range()
.
To solve the error, either pass the length of the list or pass an integer to the function.
Here is an example of how the error occurs.
my_list = ['a', 'b', 'c'] # ๐๏ธ Expects to get called with an integer but is called with a list # โ๏ธ TypeError: 'list' object cannot be interpreted as an integer for i in range(my_list): print(i)
range
function expects an integer as an argument, however, we called the function with a list.To solve the error, we have to pass an integer instead of a list to the function.
For example, you can pass the length of the list as an argument to the function.
my_list = ['a', 'b', 'c'] for i in range(len(my_list)): print(i) # ๐๏ธ 0, 1, 2 print(len(my_list)) # ๐๏ธ 3
The len() function returns the length (the number of items) of an object.
We passed the length of the list (3) to the range()
function to create a range
object that starts at 0
and goes to 2
.
If you meant to iterate over the list, remove the call to range()
.
my_list = ['a', 'b', 'c'] # โ Iterating over a list for item in my_list: print(item) # ๐๏ธ 'a', 'b', 'c'
If you need access to the index of the current iteration when iterating over a
list, use the enumerate()
function.
my_list = ['a', 'b', 'c'] # โ Iterate over a list with access to the index for idx, item in enumerate(my_list): print(idx, item) # ๐๏ธ 0 a, 1 b, 2 c
The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.
range()
function takes integers as arguments, but we passed a list to the function.The range() function is commonly used for
looping a specific number of times in for
loops and takes the following
parameters:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
print(list(range(3))) # ๐๏ธ [0, 1, 2] print(list(range(1, 4))) # ๐๏ธ [1, 2, 3] print(list(range(1, 4))) # ๐๏ธ [1, 2, 3]
If you only pass a single argument to the range()
constructor, it is
considered to be the value for the stop
parameter.
list.pop()
function with a list instead of an integerMake sure you aren't calling the list.pop()
method with a list instead of an
integer.
a_list = ['a', 'b', 'c'] # โ๏ธ TypeError: 'list' object cannot be interpreted as an integer a_list.pop([1])
The list.pop
method removes the item at the given position in the list and
returns it.
You should pass an integer in the call to the pop()
method to solve the error.
a_list = ['a', 'b', 'c'] a_list.pop(1) print(a_list) # ๐๏ธ ['a', 'c']
If no index is specified, the pop()
method removes and returns the last item
in the list.
my_list = ['bobby', 'hadz', 'com'] my_list.pop() print(my_list) # ๐๏ธ ['bobby', 'hadz']
len(my_list)
as an argument to the function or figure out where the variable gets assigned a list in your code.Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.
my_int = 10 # ๐๏ธ Reassigned a variable to a list by mistake my_int = ['a', 'b', 'c'] # ๐๏ธ Function expects to be called with an integer argument # โ๏ธ TypeError: 'list' object cannot be interpreted as an integer result = range(my_int)
We initially set the my_int
variable to an integer but later reassigned it to
a list.
You have to track down where the variable got assigned a list and correct the assignment.
You can use the type
and isinstance
functions if you aren't sure what type a
variable stores.
a_list = ['a', 'b', 'c'] print(type(a_list)) # ๐๏ธ <class 'list'> print(isinstance(a_list, list)) # ๐๏ธ True an_int = 100 print(type(an_int)) # ๐๏ธ <class 'int'> print(isinstance(an_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: 'tuple' object cannot be interpreted as an integer" occurs when we pass a tuple to a function that expects an integer argument.
To solve the error, either unpack the tuple or access the tuple element at a specific index.
Here is an example of how the error occurs.
import numpy as np my_tuple = (2, 4) # โ๏ธ TypeError: 'tuple' object cannot be interpreted as an integer print(np.random.randn(my_tuple))
And here is another example.
my_tuple = (2, 4) # โ๏ธ TypeError: 'tuple' object cannot be interpreted as an integer for i in range(my_tuple): print(i)
We have a tuple that contains 2 elements but the random.randn()
and range()
functions expect integer arguments instead of a tuple.
my_tuple = (1, 2) print(my_tuple) # ๐๏ธ (1, 2) print(type(my_tuple)) # ๐๏ธ <class 'tuple'>
One way to solve the error is to access the tuple at a specific index if your tuple stores integers.
import numpy as np my_tuple = (2, 4) # โ Works print(np.random.randn(my_tuple[0], my_tuple[1]))
And, here is an example that uses the built-in range()
function.
my_tuple = (2, 4) # โ Works for i in range(my_tuple[0], my_tuple[1]): print(i)
If your tuple stores exactly as many elements as the function's arguments, you can unpack the tuple in the call to the function.
import numpy as np my_tuple = (2, 4) # โ Works print(np.random.randn(*my_tuple)) # โ Works for i in range(*my_tuple): print(i)
You can imagine that the *my_tuple
syntax unpacks the two elements of the
tuple and passes them as individual arguments to the function.
Make sure you aren't declaring a variable that stores an integer initially and overriding it somewhere in your code.
my_int = 10 my_int = 15, # ๐๏ธ Note: the trailing comma creates a tuple print(type(my_int)) # ๐๏ธ <class 'tuple'> # โ๏ธ TypeError: 'tuple' object cannot be interpreted as an integer for i in range(my_int): print(i)
We initially set the my_int
variable to an integer but later reassigned it to
a tuple.
In this case, you have to track down where the variable got assigned a tuple and correct the assignment.
Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorThe Python "TypeError: 'NoneType' object cannot be interpreted as an integer"
occurs when we pass a None
value to a function that expects an integer
argument.
To solve the error, track down where the None
value comes from and pass an
integer to the function.
Here is an example of how the error occurs.
# โ๏ธ TypeError: 'tuple' object cannot be interpreted as an integer for i in range(None): print(i)
We passed a None
value to the range()
constructor which expects to get
called with an integer.
You can check if the variable doesn't store a None value before calling the function.
my_num = None if my_num is not None: for i in range(my_num): print(i) else: print('variable is None')
The if
block will run only if the my_num
variable doesn't store a None
value, otherwise, the else
block runs.
You can also provide a default value of the variable is None
.
my_num = None if my_num is None: my_num = 3 for i in range(my_num): print(i) # ๐๏ธ 0, 1, 2
We check if the variable is None
and if it is, we set the variable to 3
.
None
valueTo solve the error, we have to figure out where the None
value comes from and
correct the assignment.
The most common source of a None
value (other than an explicit assignment) is
a function that doesn't return anything.
# ๐๏ธ this function returns None def get_num(a, b): print(a + b) # โ๏ธ TypeError: 'NoneType' object cannot be interpreted as an integer for i in range(get_num(5, 5)): print(i)
Notice that our get_num
function doesn't explicitly return a value, so it
implicitly returns None.
Make sure to return a value from the function.
def get_num(a, b): return a + b for i in range(get_num(1, 2)): print(i) # ๐๏ธ 0, 1, 2
We used a return statement to return a
value from the function and passed an integer to the range()
function.
The error occurs for multiple reasons:
None
implicitly).None
.None
.Another common cause of the error is having a function that returns a value only if a condition is met.
def get_num(a): if a > 100: return a result = get_num(5) print(result) # ๐๏ธ None
The if
statement in the get_num
function is only run if the passed in
argument is greater than 100
.
None
.To solve the error in this scenario, you either have to check if the function
didn't return None
or return a default value if the condition is not met.
def get_num(a): if a > 100: return a return 0 # ๐๏ธ returns 0 if condition not met result = get_num(5) print(result) # ๐๏ธ 0
Now the function is guaranteed to return a value regardless of whether the condition is met.