▲ | quietbritishjim 9 hours ago | |||||||
> ... confusing as hell to "newcomers". For example, when a list comprehension has multiple `for`s, what order are they nested in? I get that this is just a rhetorical question to make a point about newcomers, and I do agree it's not immediately obvious, but for the record: you can imagine any "if"s and "for"s in a list comprehension are nested statements in the same order. So, for example, this:
Is equivalent to this:
So the least comprehension is basically in left to right order with the one exception of the actual expression to be added to the list (like y.foo() in the example above). | ||||||||
▲ | creata 9 hours ago | parent [-] | |||||||
Yeah, I know, I know. But I imagine many people would mentally want to bracket [e for x in xs for y in ys] like [(e for x in xs) for y in ys] and thus conclude that y is the outer loop. | ||||||||
|