How should an iterable
be unpacked into a mismatching number of variable(s)?
Too many values:
>>> one,two = [1,2,3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
can be ignored with
>>> one,two,*_ = [1,2,3,4]
>>>
Note: "extended iterable unpacking" since Python 3 only. About the underscore.
How can the opposite case of too few data / more variables:
>>> one,two,three = [1,2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
>>>
be handled, specifically so that the remaining variables are assigned None
(or some other value)?
Something like this:
>>> one,two,three = [1,2] or None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
>>>
>>> one,two,three = [1,2] + [None]*(3-2)
>>> one
1
>>> two
2
>>> three
>>>
Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.
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.
Conclusion. The “valueerror: too many values to unpack (expected 2)” error occurs when you do not unpack all the items in a list. This error is often caused by trying to iterate over the items in a dictionary. To solve this problem, use the items() method to iterate over a dictionary.
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.
The Python error “too many values to unpack” occurs when you try to extract a number of values from a data structure into variables that don’t match the number of values. For example, if you try to unpack the elements of a list into variables whose number doesn’t match the number of elements in the list.
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. We’re going to write a program that calculates the total sales of a product at a cheese store on a given day.
Iterable objects like lists can be “unpacked”. This lets you assign the values from an iterable object into multiple variables.
You can unpack a sequence to three variable using:
one, two, *three = [1,2]
At this point, three
will be an empty list. You can then assign three
to None
using an or
check if three is empty.
three = three or None
Use the *
operator and fill an intermediate iterable with that which you're unpacking and fill the remainder with your default value of choice.
x = [1, 2]
default_value= None
one, two, three = [*x, *([default_value] * (3 - len(x)))]
And a bonus function to handle both cases:
def unpack(source, target, default_value=None):
n = len(source)
if n < target:
return [*source, *([default_value] * (target - len(source)))]
elif n > target:
return source[0:target]
else:
return source
Amend to handle non-iterable input as needed.
Does this work for you? You can make the nones
array as big as you need.
nones = [None]*100 # or however many you think you might need
one, two, three, four, five, *_ = [1,2] + nones
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With