7
x,y,z = [1,2,3], [4,5,6], [7,8,9]
for a,b,c in x,y,z:
 print(a,b,c)

The output is :

1 2 3
4 5 6
7 8 9

I can't mentally navigate whatever logic is going on here to produce this output. I am aware of the zip function to make this code behave in the way I clearly intend it to; but I'm just trying to understand why it works this way when you don't use the zip function.

Is this a deliberate functionality, a feature, that you can successively iterate through multiple lists this way? Sort of?

asked Oct 16, 2013 at 4:40
2
  • 2
    "the way I clearly intend"? LOL - I have no idea what you intend here ;-) Commented Oct 16, 2013 at 4:44
  • 2
    I'm guessing OP wants 1 4 7 etc Commented Oct 16, 2013 at 4:52

7 Answers 7

8

You have good answers already, but I think considering this equivalent variation will help to make it clearer:

x,y,z = [1,2,3], [4,5,6], [7,8,9]
for t in x,y,z:
 a, b, c = t
 print(a,b,c)

You're not surprised that t is successively bound to x, y and z, right? Exactly the same thing is happening in your original code, except that the:

a, b, c = t

part isn't as obvious.

answered Oct 16, 2013 at 4:51
Sign up to request clarification or add additional context in comments.

3 Comments

I had to read a, b, c = t (which is just weird to read at first) like three times, but then it clicked.
Great! I figured that was the part that was the real source of the confusion, so broke it out into its own line. Now you'll never be surprised by anything ever again ;-)
EVER, in ANY field of knowledge that I choose to explore. I'll always remember tonight as the night it all turned around for me.
6

Oh man this is a mess. This is simply too much use of python's iterable unpacking. The statement a, b, c = iterable simply assigns the elements of iterable to the variables a, b, and c. In this case iterable must have 3 elements.

First you have:

x,y,z = [1,2,3], [4,5,6], [7,8,9]
# Which is:
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]

then you have:

for a, b, c in x, y, z:
 print(a, b, c)
# Which is:
temp = (x, y, z)
for item in temp:
 a = item[0]
 b = item[1]
 c = item[2]
 print(a, b, c)

One more thing to note is that the statement mytuple = 1, 2, 3 is the same as mytuple = (1, 2, 3).

answered Oct 16, 2013 at 4:42

1 Comment

This answer in conjunction with Tim Peters' answer makes it crystal clear, thank you.
2

Its pretty straight forward code really.

  1. This assigns the three lists to x, y, and z.

    x,y,z = [1,2,3], [4,5,6], [7,8,9]
    
  2. This creates a tuple of (x,y,z) and will iterate over each element.

    for a,b,c in x,y,z:
    

    However, the a,b,c means that the iterables are expected to have 3 objects

  3. Then, this prints a,b and c.

     print(a,b,c)
    

If you want to see what is happening, I'd suggest altering one of the elements in y:

x,y,z = [1,2,3], [3,4,5,6], [7,8,9]
for a,b,c in x,y,z:
 print(a,b,c)
1 2 3
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

Or, by removing one from x:

x,y,z = [1,2], [4,5,6], [7,8,9]
for a,b,c in x,y,z:
 print(a,b,c)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
answered Oct 16, 2013 at 4:44

1 Comment

With Tim's answer, It is better clear to me, last example are very helpful.
2
x,y,z = [1,2,3], [4,5,6], [7,8,9]

is the same as saying

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]

Next

for a,b,c in x,y,z:

is equivalent to

for a,b,c in [x,y,z]:

which just says "take x, y and z in turn. Assign their contents, respectively to a, b and c; i.e. a=x[0], b=x[1], c=x[2].
Ultimately, this turns into

a,b,c = x
print(a,b,c)
a,b,c = y
print(a,b,c)
a,b,c = z
print(a,b,c)
answered Oct 16, 2013 at 4:45

1 Comment

much helpful but I think for a,b,c in [x,y,z]: should be written as for a, b, c in (x, y, z):, I mean tuple instead of list, No?
2

It is an unusual python thing. The implicit creation of tuple.

Here, you create an anonymous tuple at the right

x,y,z = [1,2,3], [4,5,6], [7,8,9]

This is a similar code:

a, b = 1, 2

that is the same:

a, b = (1, 2)

or

 a = 1
 b = 2

It allows a common python trick (idiom). You can swap values without an temporary variable:

a, b = b, a

The same happens interacting the key and values of a dictionary:

for i, j in my_dict.items():
 print i, j

In your code, the another temporary tuple is being created in the for loop:

for a,b,c in (x,y,z):
 print(a,b,c)

That means:

for a, b,c in ([1,2,3], [4,5,6], [7,8,9]):
 print(a,b,c)

In other words: rewrite this code for something more legible. Python isn't following its own mantra: Explicit is better than implicit..

BTW, see the fun Python Zen typing import this in a Python shell.

answered Oct 16, 2013 at 4:45

Comments

1

Everything is explained here: http://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

The first part is realizing that commas implicitly create tuples. So the first line is equivalent to:

x,y,z = ([1,2,3], [4,5,6], [7,8,9])

This also means that your for loop is equivalent to:

for a,b,c in ([1,2,3], [4,5,6], [7,8,9]):

The second part to understand is sequence unpacking. This means that if you assign a sequence of length n to n variables, Python assigns the items in the sequence appropriately. So the first part is effectively:

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]

And the for loop is the same as:

for t in (x,y,z):
 a = t[0]
 b = t[1]
 c = t[2]
 print(a,b,c)
answered Oct 16, 2013 at 4:45

Comments

0

It looks like you have 3 arrays

x = [1,2,3]
y = [4,5,6]
z = [7,8,9]

and a,b,c represent the elements in each array. So it looks like the for loop is iterating over 3 arrays and mapping the elements to a,b, and c. Then printing them out. Then again, I don't know python

answered Oct 16, 2013 at 4:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.