Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpacking a tuple in for loop

Tags:

python

I'm having some trouble with unpacking tuples. Specifically, I don't know why this doesn't work:

a = [0,1,2,3]
b = [4,5,6,7]
p = a,b

for i,k in p:
    print i,k

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-422-2ba96d641daa> in <module>()
----> 1 for i,k in p:
  2     print i,k
  3 

ValueError: too many values to unpack

It seems to me like the above code should unpack the two lists in the tuple p into i and k, but that's clearly not what's happening and I'm not sure why.

So, I guess I have two questions:

  1. What does for i,k in p actually do, if it doesn't unpack the lists in the tuple
  2. How do I easily unpack the lists from the tuple in a for loop?

The expected result is:

[0,1,2,3]
[4,5,6,7]

I'm using python 2.7.9, if this happens to be version specific.

like image 559
C_Z_ Avatar asked Dec 02 '22 16:12

C_Z_


2 Answers

If you go step-by-step...

First, doing p = a, b will get you a tuple consisting of exactly 2 elements -- your lists:

>>> a = [0, 1, 2, 3]
>>> b = [4, 5, 6, 7]
>>> p = a, b
>>> print p
([0,1,2,3], [4, 5, 6, 7])

Then, when you do for i, k in p, Python will attempt to get the first item inside p then unpack it into i, k. So, then, the first iteration of your loop is basically doing something equivalent to this:

>>> temp = p[0]
>>> print temp
[0, 1, 2, 3]
>>> i, j = temp
Traceback (most recent call last):
  File "<stdin>", line 1 in <module>
ValueError: too many values to unpack

This will fail, since what you're basically trying to do is i, k = [0, 1, 2, 3], and there are more elements in the list then there are variables.

You may want to use the zip function instead, which pairs up the numbers inside both lists.

For example:

>>> p = zip(a, b)
>>> print p
[(0, 4), (1, 5), (2, 6), (3, 7)]

That way, when we run your loop, the number of elements inside the first tuple matches the number of variables in your loop, so Python will not throw an error. That means that your output would be:

0 4
1 5
2 6
3 7

If that's not your desired output, you need to rethink the structure of your code.


Edit:

Based on your update, if you just want your output to be:

[0, 1, 2, 3]
[4, 5, 6, 7]

...then you don't actually need to use unpacking at all. The following loop would work just fine:

for i in p:
    print i

i would be assigned to the first item inside p in the first iteration, then to the second item when the loop repeats.

like image 137
Michael0x2a Avatar answered Dec 05 '22 06:12

Michael0x2a


p is (a,b).

Since you're iterating over it using a for loop, you are getting each item in the tuple one by one:

for x in p: assigns a to x, then b to x. In this case your x is i,k. Both a and b are lists of 4 items, so cannot be assigned to two items. You could do for i,j,k,l in p, which would give you the four items in different variables.

This, however, would work: i, k = p, since p is a tuple of two items.

like image 35
Max Avatar answered Dec 05 '22 06:12

Max