How to Solve Python TypeError: cannot unpack non-iterable NoneType object

by | Programming, Python, Tips

In Python, you can unpack iterable objects and assign their elements to multiple variables in the order that they appear. If you try to unpack a NoneType object, you will throw the error TypeError: cannot unpack non-iterable NoneType object. A NoneType object is not a sequence and cannot be looped or iterated over.

To solve this error, ensure you do not assign a None value to the variable you want to unpack. This error can happen when calling a function that does not return a value or using a method like sort(), which performs in place.

This tutorial will go through the error in detail and an example to learn how to solve it.


TypeError: cannot unpack non-iterable NoneType object

What is a TypeError?

TypeError occurs in Python when you perform an illegal operation for a specific data type. NoneType is the type for an object that indicates no value. None is the return value of functions that do not return anything. Unpacking is only suitable for iterable objects,

What is Unpacking in Python?

Unpacking is the process of splitting packed values into individual elements. The packed values can be a string, list, tuple, set or dictionary. During unpacking, the elements on the right-hand side of the statement are split into the values on the left-hand side based on their relative positions. Let’s look at the unpacking syntax with an example:

values = [40, 80, 90]

x, y, z = values

print(f'x: {x}, y: {y}, z: {z}')

The above code assigns the integer values in the value list to three separate variables. The value of x is 40, y is 80, and the value of z is 90. Let’s run the code to get the result:

x: 40, y: 80, z: 90

You can also unpack sets and dictionaries. Dictionaries are only ordered for Python version 3.7 and above but are unordered for 3.6 and below. Generally, it is not recommended to unpack unordered collections of elements as there is no guarantee of the order of the unpacked elements.

You cannot unpack a None value because it is not an iterable object, and an iterable is a Python object that we can use as a sequence. You may encounter this error by unpacking the result from a function that does not have a return statement.

Example

Let’s look at an example of a list of weights in kilograms that we want to sort and then unpack and print to the console.

# List of weights

weights = [100, 40, 50]

# Sort the list

weights = weights.sort()

# Unpack

a, b, c = weights

# Print Values to Console

print('Light: ', a)

print('Medium: ', b)

print('Heavy: ', c)

Let’s run the code to see the result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
      9 # Unpack
     10 
---≻ 11 a, b, c = weights
     12 
     13 # Print Values to Console

TypeError: cannot unpack non-iterable NoneType object

The program throws the error because the sort() method sorts the list of weights in place and returns None. However, we assigned the result of sort() to the variable weights, so when we try to unpack weights, we attempt to unpack a None value, which is not possible.

Solution

To solve the error, we need to ensure we are not assigning a None value to the list variable weights. Let’s look at the revised code:

# List of weights

weights = [100, 40, 50]

# Sort the list

weights.sort()

# Unpack

a, b, c = weights

# Print Values to Console

print('Light: ', a)

print('Medium: ', b)

print('Heavy: ', c)

Instead of assigning the sort() function result to the weights variable, we sort the list in place. Let’s run the code to see what happens:

Light:  40
Medium:  50
Heavu:  100

The program successfully sorts the list of weights and unpacks them into three variables, and prints the values to the console.

Summary

Congratulations on reading to the end of this tutorial. The NoneType object is not iterable, and when you try to unpack it, we will throw the error: “TypeError: cannot unpack non-iterable NoneType object”. A common cause of this error is when you try to unpack values from a function that does not return a value. To solve this error, ensure that the value you attempt to unpack is a sequence, such as a list or a tuple.

For more reading on what you can do with iterable objects go to the article called: How to Use the Python Map Function.

For more reading on cannot unpack non-iterable object errors, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨