Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack value(s) into variable(s) or None (ValueError: not enough values to unpack) [duplicate]

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)
>>>


https://stackoverflow.com/a/8857846/1619432 suggests expanding the list:
>>> one,two,three = [1,2] + [None]*(3-2)
>>> one   
1
>>> two
2
>>> three
>>>
like image 656
handle Avatar asked Dec 12 '19 11:12

handle


People also ask

How do I fix not enough values to unpack?

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.

How do I fix not enough values to unpack Python?

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.

What does too many values to unpack mean Python?

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.

What is “ValueError not enough values to unpack”?

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.

What does too many values to unpack mean in Python?

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.

Do you have to unpack every item in an iterable?

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.

What is unpacked iterable in Python?

Iterable objects like lists can be “unpacked”. This lets you assign the values from an iterable object into multiple variables.


3 Answers

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
like image 56
James Avatar answered Oct 19 '22 19:10

James


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.

like image 5
pkfm Avatar answered Oct 19 '22 19:10

pkfm


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
like image 4
igg Avatar answered Oct 19 '22 19:10

igg