The TypeError: cannot unpack non-iterable NoneType object occurs when we try to unpack the values from the method that does not return any value or if we try to assign the None value while unpacking the iterables.
In this tutorial, we will look into what exactly is TypeError: cannot unpack non-iterable NoneType object and how to resolve the error with examples.
What is Unpacking in Python?
In Python, the function can return multiple values, and it can be stored in the variable. This is one of the unique features of Python when compared to other languages such as C++, Java, C#, etc.
Unpacking in Python is an operation where an iterable of values will be assigned to a tuple or list of variables.
Example of List Unpacking
x,y,z = [5,10,15]
print(x)
print(y)
print(z)
Output
5
10
15
What is TypeError: cannot unpack non-iterable NoneType object
First, let us see how to reproduce this issue and why developers face this particular issue with a simple example.
# list of cars
my_cars = ["Ford", "Audi", "BMW"]
# sort the cars in Alphabetical Order
my_cars = my_cars.sort()
# unpack cars into different variables
a, b, c = my_cars
print("Car", a)
print("Car", b)
print("Car", c)
Output
Traceback (most recent call last):
File "c:\Personal\IJS\Code\code22.py", line 8, in <module>
a, b, c = my_cars
TypeError: cannot unpack non-iterable NoneType object
In the above example, we have a simple list, and we are sorting the list in alphabetical order and then unpacking them into different variables.
The sort()
function sorts the elements in alphabetical order by modifying the same object and does not return any value. The return type will be considered as None
value.
The sort()
method returns None value and here we are assigning the None
value to the my_cars
variable.
We are unpacking the None
value into each of these variables, which results in TypeError: cannot unpack non-iterable NoneType object
The same can happen when we use the user-defined function, which returns the None
Value or if you have forgotten the return statement inside your function. In simple terms, any method that does not return a value or if it returns the value of None will lead to this error.
How to resolve TypeError: cannot unpack non-iterable NoneType object
We can resolve the cannot unpack non-iterable NoneType object by ensuring the unpacked items does not contain None
values while assigning to the variables.
Let us see the various scenarios and the solutions for each scenario.
Scenario 1: Unpacking iterables with built-in methods
In the case of unpacking iterables we need to ensure that we are not using any built-in methods that return None
while assigning it to variables.
In our above example, we have used the sort()
method, which returns None
value, and because of it, we get the TypeErrror.
As shown below, we can resolve the issue by first sorting the list and then unpacking the items. We have not assigned the None
value to any variable here. The sort()
doesn’t return any value however it modifies the original list into alphabetical order here. Hence unpacking the list works as expected in this case.
# list of cars
my_cars = ["Ford", "Audi", "BMW"]
# sort the cars in Alphabetical Order
my_cars.sort()
# unpack cars into different variables
a, b, c = my_cars
print("Car", a)
print("Car", b)
print("Car", c)
Output
Car Audi
Car BMW
Car Ford
.
Scenario 2: Unpacking user-defined function which returns multiple values
If you have written a custom function that returns multiple values, you need to ensure that the return value should not be of None
value while unpacking the items.
Also, we need to ensure that the function has a proper return statement defined; else, it could also lead to the TypeError: cannot unpack non-iterable NoneType object.
def custom_function(a, b):
return a
x, y = custom_function(5, 10)
print(x)
print(y)
Output
Traceback (most recent call last):
File "c:\Personal\IJS\Code\code22.py", line 5, in <module>
x, y = custom_function(5, 10)
TypeError: cannot unpack non-iterable int object
Solution
Here we need to return both a and b with proper values as we are unpacking 2 items. If any of it is None
or if we do not return 2 values then you will get an TypeError exception again.
def custom_function(a, b):
return a,b
x, y = custom_function(5, 10)
print(x)
print(y)
Output
5
10
Conclusion
The TypeError: cannot unpack non-iterable NoneType object occurs when we try to unpack the values from the method that returns multiple values as None
value or if we try to assign the None
value while unpacking the iterables.
We can resolve the issue by ensuring the function has a proper return statement.
Also, when we try to unpack the values from a method and assign the values to the variables, we need to make sure the values are not of type None
.