Last updated: Apr 8, 2024
Reading timeยท9 min
The Python "TypeError: cannot unpack non-iterable int object" occurs when we try to unpack an integer value.
To solve the error, track down where the variable got assigned an integer and correct the assignment to an iterable, e.g. a list or a tuple of integers.
Here is an example of how the error occurs.
# โ๏ธ TypeError: cannot unpack non-iterable int object a, b = 10
We are trying to unpack an integer, but integers are not iterable.
You can use a tuple or a list of integers.
a, b = (10, 20) print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20
To be able to use unpacking, the variables need to be exactly as many as the values in the iterable.
You can use this approach if you need to initialize multiple variables to 0
.
a, b, c = 0, 0, 0 print(a, b, c) # ๐๏ธ 0, 0, 0
The same approach can be used to unpack a list.
a, b = [10, 20] print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20
If you are unpacking the result of calling a function, make sure to return a tuple or a list of integers from the function.
def get_list(): return [10, 20] a, b = get_list() print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20
The get_list
function returns a list of integers, so we can unpack the
integers into variables.
If you need to assign individual digits of an integer to variables, convert the integer to a string.
a, b, c = str(123) print(a) # ๐๏ธ '1' print(b) # ๐๏ธ '2' print(c) # ๐๏ธ '3'
Unlike integers, strings are iterable. Note that the individual variables store strings, so you'll have to pass them to the int() class if you need to convert them back to integers.
Use an if
statement if you need to check whether a variable doesn't store an
integer before unpacking.
example = 10 if not isinstance(example, int): a, b = example print(a, b) else: # ๐๏ธ this runs print('Variable stores an integer')
The variable stores an integer, so the else
block runs.
Alternatively, you can reassign the variable to an iterable if it stores an integer.
example = 10 if isinstance(example, int): example = (0, 0) a, b = example print(a, b) # ๐๏ธ 0, 0
We check if the example
variable stores an integer and if it does, we reassign
it to a tuple.
Another common cause of the error is trying to iterate over a list of integers with access to the index.
a_list = [10, 20, 30, 40] # โ๏ธ TypeError: cannot unpack non-iterable int object for index, item in a_list: print(index, item)
The code sample causes the error because we try to unpack one of the integers in the list.
Instead, use the enumerate()
function to iterate over a list with access to
the index of the current iteration.
a_list = [10, 20, 30, 40] for index, item in enumerate(a_list): print(index, item) # 0 10, 1 20, 2 30, ...
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.
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐๏ธ 0 bobby, 1 hadz, 2 com
If you aren't sure what type of object a variable stores, use the type()
class.
my_int = 10 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True my_list = [0, 0] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ 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: cannot unpack non-iterable function object" occurs when we try to unpack a function object instead of an iterable.
To solve the error, make sure to call the function with parentheses, e.g.
my_function()
and return an iterable from the function.
Here is an example of how the error occurs.
def get_list(): return ['one', 'two'] # โ๏ธ TypeError: cannot unpack non-iterable function object a, b = get_list # ๐๏ธ forgot to call function
get_list()
, so our code actually tries to unpack the function object and not the list.To solve the error, make sure to call the function.
def get_list(): return ['one', 'two'] a, b = get_list() print(a) # ๐๏ธ 'one' print(b) # ๐๏ธ 'two'
We used parentheses to invoke the function, so now we iterate over the list that the function returns.
Note that the values on the left of the assignment have to be exactly as many as there are items in the list.
my_func(10, 20)
.def get_list(x, y): return [x, y] a, b = get_list(10, 20) print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20
Make sure to return an iterable from the function, e.g. a list or a tuple.
a, b = (10, 20) print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20
The variables need to be exactly as many as the values in the iterable.
a, b, c = (10, 20, 30) print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20 print(c) # ๐๏ธ 30
If the values on the left are more or fewer than the values in the iterable, an error is raised.
If you aren't sure what type of object a variable stores, use the type()
class.
def get_list(): return ['one', 'two'] print(type(get_list)) # ๐๏ธ <class 'function'> print(callable(get_list)) # ๐๏ธ True my_list = ['a', 'b', 'c'] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ 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 callable function takes an
object as an argument and returns True
if the object appears callable,
otherwise, False
is returned.
If the callable()
function returns True
, it is still possible that calling
the object fails, however, if it returns False
, calling the object will never
succeed.
The Python "TypeError: cannot unpack non-iterable float object" occurs when we try to unpack a float value.
To solve the error, track down where the variable got assigned a float and correct the assignment to an iterable, e.g. a list or a tuple of floats.
Here is an example of how the error occurs.
my_float = 3.1 # โ๏ธ TypeError: cannot unpack non-iterable float object a, b = my_float
We are trying to unpack a floating-point number, but floating-point numbers are not iterable.
You can use a tuple or a list of floats.
my_tuple = (3.1, 6.2) a, b = my_tuple print(a) # ๐๏ธ 3.1 print(b) # ๐๏ธ 6.2
The variables need to be exactly as many as the values in the iterable.
If there are more or fewer values on the left-hand side than there are on the right-hand side of the assignment, an error is raised.
You can use this approach if you need to initialize multiple variables to 0
.
a, b, c = 0, 0, 0 print(a, b, c) # ๐๏ธ 0, 0, 0
If you are unpacking the result of calling a function, make sure to return a tuple or a list of floats from the function.
def get_list(): return [3.1, 6.2] a, b = get_list() print(a) # ๐๏ธ 3.1 print(b) # ๐๏ธ 6.2
The get_list
function returns a list of floats, so we can unpack the floats
into variables.
Use an if
statement if you need to check whether a variable doesn't store a
float before unpacking.
example = 3.1 if not isinstance(example, float): a, b = example else: # ๐๏ธ this runs print('Variable stores a float')
Alternatively, you can reassign the variable to an iterable if it stores a floating-point number.
example = 3.1 if isinstance(example, float): example = (0, 0) a, b = example print(a, b) # ๐๏ธ 0, 0
We check if the example variable stores a floating-point number.
If it does, we reassign it to a tuple containing 2 values.
If you aren't sure what type of object a variable stores, use the type()
class.
my_float = 3.14 print(type(my_float)) # ๐๏ธ <class 'float'> print(isinstance(my_float, float)) # ๐๏ธ True my_list = [3.1, 6.2] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ 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: cannot unpack non-iterable NoneType object" occurs when
we try to unpack a None
value.
To solve the error, track down where the variable got assigned a None value and correct the assignment to an iterable, e.g. a list or a tuple.
Here is an example of how the error occurs.
# โ๏ธ TypeError: cannot unpack non-iterable NoneType object a, b = None
We are trying to unpack a None
value, but None
values are not iterable.
None
If you need to declare multiple variables that store None
on the same line,
separate multiple None
values by commas.
a, b = None, None print(a) # ๐๏ธ None print(b) # ๐๏ธ None
The number of values on the left-hand side and the right-hand side of the assignment has to match.
If there are more or fewer values on the left-hand side, an error is raised.
Use an if
statement if you need to
check whether a variable doesn't store a None
value before unpacking it.
example = None if example is not None: a, b = example else: # ๐๏ธ this runs print('variable stores a None value')
The if
block runs only if the variable doesn't store a None
value, so no
error is raised.
Alternatively, you can reassign the variable to a different value if it stores
None
.
example = None if example is None: example = [10, 20, 30] a, b, c = example print(a) # ๐๏ธ 10 print(b) # ๐๏ธ 20 print(c) # ๐๏ธ 30
If the variable stores None, we set it to a list containing 3 values and unpack the values.
To solve the error, you have to track down where the variable got assigned a
None
value and correct the assignment.
The most common sources of None
values are:
None
implicitly).None
.None
in PythonFunctions that don't explicitly return a value return None
.
# ๐๏ธ this function returns None def get_list(): print(['one', 'two']) # โ๏ธ TypeError: cannot unpack non-iterable NoneType object a, b = get_list()
You can use a return statement to return a value from a function.
def get_list(): return ['one', 'two'] a, b = get_list() print(a) # ๐๏ธ 'one' print(b) # ๐๏ธ 'two'
The function now returns a list containing 2 elements.
Note that there are many built-in functions (e.g. sort()
) that mutate the
original object in place and return None
.
Make sure you aren't storing the result of calling one in a variable.
my_list = ['c', 'b', 'a'] # โ๏ธ TypeError: cannot unpack non-iterable NoneType object x, y, z = my_list.sort()
The sort
functions and many other built-in
functions return None
because they mutate the
original object.
Instead, call sort()
on the list and unpack the values after.
my_list = ['c', 'b', 'a'] my_list.sort() x, y, z = my_list print(x) # ๐๏ธ a print(y) # ๐๏ธ b print(z) # ๐๏ธ c
Another common cause of the error is having a function that returns a value only if a condition is met.
def get_list(l): if len(l) > 3: return l my_list = get_list(['one', 'two', 'three']) print(my_list) # ๐๏ธ None # โ๏ธ TypeError: cannot unpack non-iterable NoneType object a, b, c = my_list
The if
statement in the get_list
function is only run if the passed in
argument has a length greater than 3
.
None
.To solve the error, 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_list(l): if len(l) > 3: return l return ['', '', ''] # ๐๏ธ return default value if condition not met my_list = get_list(['one', 'two', 'three']) print(my_list) # ๐๏ธ ['', '', ''] a, b, c = my_list print(a, b, c) # ๐๏ธ "", "", ""
Now the function is guaranteed to return a value regardless of whether the condition is met.