Reputation: 37018
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?
Upvotes: 7
Views: 250
Reputation: 39153
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.
Upvotes: 2
Reputation: 25813
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)
.
Upvotes: 6
Reputation: 70582
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.
Upvotes: 8
Reputation: 113905
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)
Upvotes: 2
Reputation: 2290
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)
Upvotes: 1
Reputation: 664
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
Upvotes: 0
Reputation:
Its pretty straight forward code really.
This assigns the three lists to x
, y
, and z
.
x,y,z = [1,2,3], [4,5,6], [7,8,9]
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
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
Upvotes: 2