variable scope in list comprehensions

Piotr Sobolewski NIE_DZIALA at gazeta.pl
Thu Apr 3 02:48:41 EDT 2008


Hello,

there is something I don't understand about list comprehensions.

I understand how does this work:
print [[y for x in range(8)] for y in range(8)]

However I don't understand why this one works:
print [[y for y in range(8)] for y in range(8)]

In this second example I have one loop nested in another. Both loops uses
the same variable - y. How is it that those loops do not conflict with each
other?

For a moment I thought that maybe list comprehension has its own scope, but
it doesn't seem to be so:
print [[y for y in range(8)] for y in range(8)]
print y

Does anybody understand it?





More information about the Python-list mailing list