TypeError: cannot unpack non-iterable 'X' object in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
9 min

banner

# Table of Contents

  1. TypeError: cannot unpack non-iterable INT object in Python
  2. TypeError: cannot unpack non-iterable FUNCTION object
  3. TypeError: cannot unpack non-iterable FLOAT object
  4. TypeError: cannot unpack non-iterable NoneType object

# TypeError: cannot unpack non-iterable int object in Python

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.

typeerror cannot unpack non iterable int object

Here is an example of how the error occurs.

main.py
# โ›”๏ธ TypeError: cannot unpack non-iterable int object a, b = 10

cause of error

We are trying to unpack an integer, but integers are not iterable.

# Unpacking a tuple or a list of integers

You can use a tuple or a list of integers.

main.py
a, b = (10, 20) print(a) # ๐Ÿ‘‰๏ธ 10 print(b) # ๐Ÿ‘‰๏ธ 20

unpack tuple or list of integers

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.

main.py
a, b, c = 0, 0, 0 print(a, b, c) # ๐Ÿ‘‰๏ธ 0, 0, 0

The same approach can be used to unpack a list.

main.py
a, b = [10, 20] print(a) # ๐Ÿ‘‰๏ธ 10 print(b) # ๐Ÿ‘‰๏ธ 20

# Unpacking the result of calling a function

If you are unpacking the result of calling a function, make sure to return a tuple or a list of integers from the function.

main.py
def get_list(): return [10, 20] a, b = get_list() print(a) # ๐Ÿ‘‰๏ธ 10 print(b) # ๐Ÿ‘‰๏ธ 20

unpack result of calling function

The get_list function returns a list of integers, so we can unpack the integers into variables.

# Assigning individual digits of an integer to variables

If you need to assign individual digits of an integer to variables, convert the integer to a string.

main.py
a, b, c = str(123) print(a) # ๐Ÿ‘‰๏ธ '1' print(b) # ๐Ÿ‘‰๏ธ '2' print(c) # ๐Ÿ‘‰๏ธ '3'

assign individual digits of integer to variables

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.

# Check if a variable doesn't store an integer before unpacking

Use an if statement if you need to check whether a variable doesn't store an integer before unpacking.

main.py
example = 10 if not isinstance(example, int): a, b = example print(a, b) else: # ๐Ÿ‘‡๏ธ this runs print('Variable stores an integer')

check if variable does not store integer before unpacking

The variable stores an integer, so the else block runs.

Alternatively, you can reassign the variable to an iterable if it stores an integer.

main.py
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.

# Iterating over a list with access to the index

Another common cause of the error is trying to iterate over a list of integers with access to the index.

main.py
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.

main.py
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.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐Ÿ‘‰๏ธ 0 bobby, 1 hadz, 2 com

# Check what type a variable stores

If you aren't sure what type of object a variable stores, use the type() class.

main.py
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

checking what type a variable stores

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.

# Table of Contents

  1. TypeError: cannot unpack non-iterable FUNCTION object
  2. TypeError: cannot unpack non-iterable FLOAT object
  3. TypeError: cannot unpack non-iterable NoneType object

# TypeError: cannot unpack non-iterable function object

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.

typeerror cannot unpack non iterable function object

Here is an example of how the error occurs.

main.py
def get_list(): return ['one', 'two'] # โ›”๏ธ TypeError: cannot unpack non-iterable function object a, b = get_list # ๐Ÿ‘ˆ๏ธ forgot to call function
We forgot to call the function with parentheses, e.g. get_list(), so our code actually tries to unpack the function object and not the list.

# Call the function with parentheses to solve the error

To solve the error, make sure to call the function.

main.py
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.

If your function takes any arguments, you have to provide them when calling it, e.g. my_func(10, 20).
main.py
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.

main.py
a, b = (10, 20) print(a) # ๐Ÿ‘‰๏ธ 10 print(b) # ๐Ÿ‘‰๏ธ 20

The variables need to be exactly as many as the values in the iterable.

main.py
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.

# Checking what type a variable stores

If you aren't sure what type of object a variable stores, use the type() class.

main.py
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.

# Table of Contents

  1. TypeError: cannot unpack non-iterable FLOAT object
  2. TypeError: cannot unpack non-iterable NoneType object

# TypeError: cannot unpack non-iterable float object

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.

typeerror cannot unpack non iterable float object

Here is an example of how the error occurs.

main.py
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.

# Unpacking a list or a tuple of floating-point numbers

You can use a tuple or a list of floats.

main.py
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.

main.py
a, b, c = 0, 0, 0 print(a, b, c) # ๐Ÿ‘‰๏ธ 0, 0, 0

# Unpacking the result of calling a function into variables

If you are unpacking the result of calling a function, make sure to return a tuple or a list of floats from the function.

main.py
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.

# Checking if a variable doesn't store a float before unpacking

Use an if statement if you need to check whether a variable doesn't store a float before unpacking.

main.py
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.

main.py
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.

# Printing the type of a variable

If you aren't sure what type of object a variable stores, use the type() class.

main.py
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.

# TypeError: cannot unpack non-iterable NoneType object

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.

typeerror cannot unpack non iterable nonetype object

Here is an example of how the error occurs.

main.py
# โ›”๏ธ TypeError: cannot unpack non-iterable NoneType object a, b = None

We are trying to unpack a None value, but None values are not iterable.

# Declaring multiple variables that store None

If you need to declare multiple variables that store None on the same line, separate multiple None values by commas.

main.py
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.

# Checking if a variable doesn't store None before unpacking

Use an if statement if you need to check whether a variable doesn't store a None value before unpacking it.

main.py
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.

main.py
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.

# Track down where the variable got set to None

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:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Functions that don't return a value return None in Python

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ 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.

main.py
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.

# Many built-in methods return None

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.

main.py
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.

main.py
my_list = ['c', 'b', 'a'] my_list.sort() x, y, z = my_list print(x) # ๐Ÿ‘‰๏ธ a print(y) # ๐Ÿ‘‰๏ธ b print(z) # ๐Ÿ‘‰๏ธ c

# Having a function that returns a value only if a condition is met

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
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.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev