Python sequences can be unpacked. This means you can assign the contents of a sequence to multiple variables. If you try to unpack a None value using this syntax, you’ll encounter the “TypeError: cannot unpack non-iterable NoneType object” error.
In this guide, we break down what this error means and why you may see it. We discuss an example of this error in action so you can figure out how to solve it.
TypeError: cannot unpack non-iterable NoneType object
Unpacking syntax lets you assign multiple variables at the same time based on the contents of a sequence. Consider the following code:
fruit_sales = [230, 310, 219] avocado, bananas, apples = fruit_sales
This code lets us assign the values in the fruit_sales variable to three separate variables. The value of “avocado” becomes 230, the value of “bananas” becomes 310, and the value of “apples” becomes 219.
The unpacking syntax only works on sequences, like lists and tuples. You cannot unpack a None value because None values are not sequences.
This error is commonly raised if you try to unpack the result of a function that does not include a return statement.
An Example Scenario
We’re going to create a program that calculates the average purchase price at a coffee shop and the largest purchase made on a given day.
Start by defining a list of purchases made on a particular day:
purchases = [2.30, 3.90, 4.60, 7.80, 2.20, 2.40, 8.30]
Next, define a function. This function will calculate the average purchase price and the largest purchase made on a given day:
def calculate_statistics(purchases): average_purchase = sum(purchases) / len(purchases) largest_purchase = max(purchases)
To calculate the average value of a purchase, divide the total value of all purchases by how many purchases are made on a given day. We use the max() function to find the largest purchase.
Now that we’ve defined our function, we can call it. We assign the values our function returns to variables:
average, largest = calculate_statistics(purchases)
We use the unpacking syntax to access these two values from our function.
This code lets us access the values our function returns in our main program. Now that we have access to these values outside our function, we print them to the console:
print("The average purchase was ${}.".format(round(average, 2))) print("The largest purchase was ${}.".format(round(largest, 2)))
Our code will show us the average purchase value and the value of the largest purchase on the console. We round both of these values to two decimal places using the round() method. Let’s run our program and see what happens:
Traceback (most recent call last): File "main.py", line 7, in <module> average, largest = calculate_statistics(purchases) TypeError: cannot unpack non-iterable NoneType object
Our code returns an error message.
The Solution
The error occurs on the line where we try to unpack the values from our function.
Our error message tells us we’re trying to unpack values from a None value. This tells us our function is not returning the correct values.
If we take a look at our function, we see we’ve forgotten a return statement. This means our code cannot unpack any values.
To solve this error, we must return the “average_purchase” and “largest_purchase” values in our function so we can unpack them in our main program:
def calculate_statistics(purchases): average_purchase = sum(purchases) / len(purchases) largest_purchase = max(purchases) return average_purchase, largest_purchase
Let’s run our code:
The average purchase was $4.5. The largest purchase was $8.3.
Our code tells us the average purchase was $4.50 and the largest purchase was $8.30.
Conclusion
The “TypeError: cannot unpack non-iterable NoneType object” error is raised when you try to unpack values from one that is equal to None.
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, make sure the value you are trying to unpack is a sequence, such as a list or a tuple.
You’re now ready to solve this error like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.