5

I tried to use the value of an outer list comprehension in an inner one:

[ x for x in range(y) for y in range(3) ]

But unfortunately this raises a NameError because the name y is unknown (although the outer list comprehension specifies it).

Is this a limitation of Python (2.7.3 and 3.2.3 tried) or is there a good reason why this cannot work?

Are there plans to get rid of the limitation?

Are there workarounds (some different syntax maybe I didn't figure out) to achieve what I want?

1
  • 2
    A good way to remember the order is to think about how you would do this without list comprehension (someone recently mentioned this, and it stuck)
    – Levon
    Aug 13, 2012 at 12:43

3 Answers 3

17

You are talking about list comprehensions, not generator expressions.

You need to swap your for loops:

[ x for y in range(3) for x in range(y) ]

You need to read these as if they were nested in a regular loop:

for y in range(3):
    for x in range(y):
        x

List comprehensions with multiple loops follow the same ordering. See the list comprehension documentation:

When a list comprehension is supplied, it consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.

The same thing goes for generator expressions, of course, but these use () parenthesis instead of square brackets and are not immediately materialized:

>>> (x for y in range(3) for x in range(y))
<generator object <genexpr> at 0x100b50410>
>>> [x for y in range(3) for x in range(y)]
[0, 0, 1]
3
  • 1
    +1 thanks Martijn, I never understood the order until now. (Might have something to do with that "not being obvious unless you're Dutch" thing.) Aug 13, 2012 at 13:22
  • 1
    I have to keep in mind that iteratively it would be for A: for B: C while in comprehensions it is not (as I expected) C for B for A but C for A for B. That's curious, I guess even if you're Dutch ;-)
    – Alfe
    Aug 13, 2012 at 13:35
  • @Alfe: I note that the PEP was written by Barry Warsaw, Bass Player Extraordinaire and All-Round American, not Guido. :-P Although all the original work was done by Greg Ewing. That's a New Zealander. Perhaps that's why.
    – Martijn Pieters
    Aug 13, 2012 at 13:42
1

Did you try:

[x for y in range(3) for x in range(y)]

Because that produces an output... This produces:

[0, 0, 1]

Which may or may not be what you wanted....

5
  • It's what I want, yes. My usecase is a bit more complicated, of course:
    – Alfe
    Aug 13, 2012 at 12:50
  • return [ value for target in targetSet for topic, value in self.db[target].keys() if topic == chosenTopic ]
    – Alfe
    Aug 13, 2012 at 12:51
  • 1
    @Levon, sometimes answers get posted at the same time. Once I hit submit, I found out others beat me to it. I then decided to add the output of running this, as the others didn't include that (yet - I haven't checked). Aug 13, 2012 at 13:13
  • actually it just became this: list(set(value for target in targetSet for topic, values in self.db[target].iteritems() if topic == chosenTopic for value in values if (topic, value) not in trail)). I wonder if anyone could guess what I'm doing now ;-)
    – Alfe
    Aug 13, 2012 at 13:40
  • And another thing: set([ topic for target in targetSet for topic, values in self.db[target].iteritems() if set((topic, value) for value in values) - trail ]). But now I'm drifting off-topic, sorry.
    – Alfe
    Aug 13, 2012 at 13:40
0

just nest another gen.

[ x for x in [range(y) for y in range(3) ]]

gives me:

[[], [0], [0, 1]]
2
  • But list of lists isn't what I need. Thanks anyway.
    – Alfe
    Aug 13, 2012 at 12:45
  • @Alfe well your question was not very clear. go with martijns answer then. :)
    – Inbar Rose
    Aug 13, 2012 at 12:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.