Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: need more than 0 values to unpack (python lists)

Tags:

I'm learning python from Google code class. I'm trying out the exercises.

def front_x(words):   x_list, ord_list = []   for word in words:     if word[0] == 'x':       x_list.append(word)     else:       ord_list.append(word)   return sorted(x_list) + sorted(ord_list)       

I believe the error is thrown because of initializing two empty lists on a single line. If if initialize them on separate lines, no more errors occur. Is this the reason?

like image 746
kunaguvarun Avatar asked Dec 15 '12 11:12

kunaguvarun


People also ask

How to solve too many values to unpack in Python?

This error occurs when the number of variables doesn't match the number of values. As a result of the inequality, Python doesn't know which values to assign to which variables, causing us to get the error ValueError: too many values to unpack . Today, we'll look at some of the most common causes for this ValueError .

What is too many values to unpack?

The ValueError: too many values to unpack (expected 2)” occurs when you do not unpack all of the items in a list. A common mistake is trying to unpack too many values into variables. We can solve this by ensuring the number of variables equals the number of items in the list to unpack.

What does ValueError too many values to unpack expected 2 mean in Python?

ValueError: too many values to unpack (expected 2) occurs when there is a mismatch between the returned values and the number of variables declared to store these values. If you have more objects to assign and fewer variables to hold, you get a value error.

What does ValueError not enough values to unpack expected 2 got 1 mean?

During a multiple value assignment, the ValueError: not enough values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects. This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement.


Video Answer


1 Answers

You are trying to use tuple assignment:

x_list, ord_list = [] 

you probably meant to use multiple assignment:

x_list = ord_list = [] 

which will not do what you expect it to; use the following instead:

x_list, ord_list = [], [] 

or, best still:

x_list = [] ord_list = [] 

When using a comma-separated list of variable names, Python expects there to be a sequence of expressions on the right-hand side that matches the number variables; the following would be legal too:

two_lists = ([], []) x_list, ord_list = two_lists 

This is called tuple unpacking. If, on the other hand, you tried to use multiple assignment with one empty list literal (x_list = ord_list = []) then both x_list and ord_list would be pointing to the same list and any changes made through one variable will be visible on the other variable:

>>> x_list = ord_list = [] >>> x_list.append(1) >>> x_list [1] >>> ord_list [1] 

Better keep things crystal clear and use two separate assignments, giving each variable their own empty list.

like image 137
Martijn Pieters Avatar answered Oct 05 '22 13:10

Martijn Pieters