In Python, we can unpack iterable objects and assign their element value to multiple variables. But if we try to unpack a NoneType object value
, we will encounter the "TypeError: cannot unpack non-iterable NoneType object" Error.
In this Python guide, we will discuss this error in detail and learn how to fix it. We will also walk through an example scenario, so you can figure out how to solve this error for yourself.
Python Problem: Python Program to Swap Two Variables
In Python unpacking, we can assign iterable object's (string, tuple, list, set, and dictionary) items to the multiple variables using a single-line statement.
# list object my_list= [20, 30, 40] # unpacking x, y, z = my_list
The above code will unpack the list
and assign the values 20 to x, 30 to y, and 40 to z.
The unpacking can only be performed using an iterable object. And if we try to perform it on a None value, we will receive the error
TypeError: cannot unpack non-iterable NoneType object
. The Error statement "TypeError: cannot unpack non-iterable NoneType object" has two parts.
- cannot unpack non-iterable NoneType object
TypeError is a Python standard exception. This exception is raised in a Python program when performing an invalid or unsupported operation on a Python object. When we perform unpacking on a None value, we receive a TypeError exception.
2. cannot unpack non-iterable NoneType object
This is the error message that tags along with the Python TypeError exception. The error message is clearly telling us that we are trying to unpack a non-iterable NoneType object , which is not supported in Python. You will only encounter this error in your Python program when you try to unpack a None value.
# None unpacking a, b, c = None
Traceback (most recent call last): File "main.py", line 2, in a, b, c = None TypeError: cannot unpack non-iterable NoneType object
Common Example Scenario
Encountering this error is common when you are trying to unpack an iterable object, and for some reason, you declare that iterable object as a None value.
Let's say you have a list
that contains three price ranges for a similar product. And need to
write a program
that sorts the
list and assigns the three prices to the
# list prices = [100, 50, 200] # sort the list prices = prices.sort() # unpack cheap, medium, expensive = prices print("Expensive: ", expensive) print("Medium: ", medium) print("Cheap: ", cheap)
Traceback (most recent call last): File "main.py", line 8, in expensive, medium, cheap = prices TypeError: cannot unpack non-iterable NoneType object
Break the code
In the above example, we are receiving an error in line 8 with
expensive, medium, cheap = prices
. This is because at that statement, the value of prices in
In line 5, we are sorting the list with statement
prices = prices.sort()
. The list sort() method sorts the list in place and returns None. At that point, the value of
and when we tried to unpack it, we received the "TypeError: cannot unpack non-iterable NoneType object" Error.
To solve the above problem, we need to make sure that we are not assigning any None value to the list
# list prices = [100, 50, 200] # sort the list prices.sort() # unpack cheap, medium, expensive = prices print("Expensive: ", expensive) print("Medium: ", medium) print("Cheap: ", cheap)
Expensive: 200 Medium: 100 Cheap: 50
In this Python tutorial, we learned why "TypeError: cannot unpack non-iterable NoneType object" is raised in a Python program. The NoneType object value None is not iterable , and when we try to unpack it, we encounter this error. To fix this error, you need to make sure that you are not unpacking the None value in your program. A common case when you encounter this error is when you unpack a return value from a method or function that returns None.
So be careful with the value you are receiving. If you are still getting this error in your Python program, you can share your code and query in the comment section. We will try to help you in debugging.
People are also reading:
- Python TypeError: object of type ‘int’ has no len() Solution
- Gmail API in Python
- Python AttributeError: A Complete Guide
- Delete Emails in Python
- Python TypeError: Method_Name() missing 1 required positional argument: ‘self’ Solution
- Numpy dot Product
- Python TypeError: can only join an iterable Solution
- String count() in Python
- Python KeyError: A Complete Guide
- np.arange() | NumPy Arange Function in Python