Unpacking syntax lets you separate the values from iterable objects. If you try to unpack more values than the total that exist in an iterable object, you’ll encounter the “ValueError: not enough values to unpack” error.
This guide discusses what this error means and why you may see it in your code. We’ll walk through an example of this error in action so you can see how it works.
ValueError: not enough values to unpack
Iterable objects like lists can be “unpacked”. This lets you assign the values from an iterable object into multiple variables.
Unpacking is common when you want to retrieve multiple values from the response of a function call, when you want to split a list into two or more variables depending on the position of the items in the list, or when you want to iterate over a dictionary using the items() method.
Consider the following example:
name, address = ["John Doe", "123 Main Street"]
This code unpacks the values from our list into two variables: name and address. The variable “name” will be given the value “John Doe” and the variable address will be assigned the value “123 Main Street”.
You have to unpack every item in an iterable if you use unpacking. You cannot unpack fewer or more values than exist in an iterable. This is because Python would not know which values should be assigned to which variables.
An Example Scenario
We’re going to write a program that calculates the total sales of a product at a cheese store on a given day. To start, we’re going to ask the user to insert two pieces of information: the name of a cheese and a list of all the sales that have been made.
We can do this using an input() statement:
name = input("Enter the name of a cheese: ") sales = input("Enter a comma-separated list of all purchases made of this cheese: ")
Our “sales” variable expects the user to insert a list of sales. Each value should be separated using a comma.
Next, define a function that calculates the total of all the sales made for a particular cheese. This function will also designate a cheese as a “top seller” if more than $50 has been sold in the last day.
def calculate_total_sales(sales): split_sales = [float(x) for x in sales.split(",")] total_sales = sum(split_sales) if total_sales > 50.00: top_seller = True else: top_seller = False return [total_sales]
We use the sum() method to calculate the total value of all the purchases for a given cheese based on the list that the
split() method returns. We then use an
if statement to determine whether a cheese is a top seller.
Our method returns an iterable with one item: the total sales made. We return an iterable so we can unpack it later in our program. Next, call our function:
total_sales, top_seller = calculate_total_sales(sales)
Our function accepts one parameter: the list of purchases. We unpack the result of our function into two parts: total_sales and top_seller.
Finally, print out the values that our function calculates:
print("Total Sales: $" + str(round(total_sales, 2)) print("Top Seller: " + str(top_seller))
We convert our variables to strings so we can concatenate them to our labels. We round the value of “total_sales” to two decimal places using the
round() method. Let’s run our program and see if it works:
Enter the name of a cheese: Edam Enter a comma-separated list of all purchases made of this cheese: 2.20, 2.90, 3.30 Traceback (most recent call last): File "main.py", line 15, in <module> total_sales, top_seller = calculate_total_sales(sales) ValueError: not enough values to unpack (expected 2, got 1)
Our program fails to execute.
calculate_total_sales() function only returns one value: the total value of all the sales made of a particular cheese. However, we are trying to unpack two values from that function.
This causes an error because Python is expecting a second value to unpack. To solve this error, we have to return the “top_seller” value into our main program:
def calculate_total_sales(sales): split_sales = [float(x) for x in sales.split(",")] total_sales = sum(split_sales) if total_sales > 50.00: top_seller = True else: top_seller = False return [total_sales, top_seller]
Our function now returns a list with two values: total_sales and top_seller. These two values can be unpacked by our program because they appear in a list. Let’s run our program and see if it works:
Enter the name of a cheese: Edam Enter a comma-separated list of all purchases made of this cheese: 2.20, 2.90, 3.30 Total Sales: $8.4 Top Seller: False
Our program now successfully displays the total sales of a cheese and whether that cheese is a top seller to the console.
The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.
Now you have the knowledge you need to fix this common Python error like an expert!