. Advertisement .
..3..
. Advertisement .
..4..
The “TypeError: cannot unpack non-iterable NoneType object” message can appear unexpectedly. It may be hard to understand this error without a good knowledge of unpacking and NoneType in Python. This is exactly what this article is going to give you.
Unpacking In Python
When Python developers create a list and assign values to its elements, this process is sometimes called packing. It means they put actual values to make a list.
>>> sites = ["ITTutoria", "Stack Overflow", "Quora"]
The statement above assigns three values to the list sites. You can then access its elements by using indexes.
>>> sites[0]
'ITTutoria'
>>> sites[1]
'Stack Overflow'
>>> sites[2]
'Quora'
This allows us to extract all the values of the list back to variables. This method works but isn’t very readable. Python has a more elegant built-in solution called unpacking, which accomplishes the same thing with less code.
>>> first, second, third = sites
What this statement does is that it pulls values from the list sites and puts them into the variables you provide in the corresponding order.
>>> first
'ITTutoria'
>>> second
'Stack Overflow'
>>> third
'Quora'
Compared to our first solution, this is far simpler and cleaner. This is crucial when you need to write bug-free code that is easy to maintain.
This unpacking operation can also be used on tuples, directions, and sets.
>>> ranking = [("Python", 1), ("Java", 2), ("JavaScript", 3)]
>>> a, b, c = ranking
>>> a
('Python', 1)
>>> b
('Java', 2)
>>> c
('JavaScript', 3)
TypeError: cannot unpack non-iterable NoneType object
The section above has demonstrated the capabilities of unpacking. However, this isn’t a universal functionality that you can carry out to every object in Python.
It is important to note that you can only unpack iterables. This term in Python indicates objects that can be iterated over, such as lists and dictionaries.
This requirement totally makes sense. Python can only do its job and unpack an object for you when it can go over the elements of that object one by one. This is the only way every value can be picked up and assigned to a variable of your choice in sequential order.
The error you are seeing occurs because you are forcing Python to unpack an object it can’t over. In this case, it is the NoneType type.
We can reproduce a simple example here:
>>> a, b = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable NoneType object
None is a Python object that is frequently used to indicate the lack of a value. It is automatically assigned by Python when, for example, you don’t pass a default argument when initializing a dictionary. None has a type of its own – NoneType. This type has no other instance other than None.
How To Fix The Problem
There are no detailed step-by-step instructions on how to remove this error because it depends on the algorithms of your program. But the approach is the same: locate the unpacking and find out why the program is unpacking the None value.
For example, let’s say we want to sort three numbers and find out which is the biggest or smallest by unpacking the list:
>>> numbers = [45, 89, 10]
>>> numbers = numbers.sort()
>>> smallest, middle, largest = numbers
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot unpack non-iterable NoneType object
The error tells us that the numbers variable actually holds a None value.
It shouldn’t come as a surprise when you understand how the method sort() works: it sorts the list and changes the values of its element accordingly, then returns a None value.
What you can do is not to get the return value of sort():
>>> numbers = [45, 89, 10]
>>> numbers.sort()
>>> smallest, middle, largest = numbers
>>> smallest
10
>>> largest
89
Conclusion
The “TypeError: cannot unpack non-iterable NoneType object” message shows up when you try to unpack a None value. Alter your code to avoid such an attempt, and the error should disappear.
Leave a comment