On 22/10/12 21:21, Saad Javed wrote:
Hi,

I'm trying to create a list (L) from items of different lists (a, b, c) but
in a specific order (L = [[a1, b1, c1], [a2, b2, c2]...etc])
L = []
a = [1, 2, 3, 4]
b = ['a', 'b', 'c', 'd']
c = [2009, 2010, 2011, 2012]

for x, y , z in zip(a, b, c):
L.extend([x, y, z])
print L

This is not your code, because that gives a SyntaxError. Where is
the indentation? Indentation is required in Python, if you leave it
out, your code will not work correctly. You should have either:

# Option 1
for x, y , z in zip(a, b, c):
    L.extend([x, y, z])
    print L


which will extend the list four times, and print the list four times,
once each time through the loop. Or:


# Option 2
for x, y , z in zip(a, b, c):
    L.extend([x, y, z])

print L  # this is not indented, so it is outside the loop



Now the list only gets printed once, after the for loop has
completely finished, and you don't see the intermediate results.



But this outputs:
[[1, 'a', 2009]]
[[1, 'a', 2009], [2, 'b', 2010]]
[[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011]]
[[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', 2012]]

I just want L = [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd',
2012]]

Then don't print the list four times, only print it once.




--
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to