Re: [Python-ideas] Order of loops in list comprehension

On Oct 22, 2016 2:51 AM, "Alexander Heger" <python@2sn.net> wrote:
A strict interpretation by this logic would also require the [x ...] part to be at the end, like [... x] since that's how it would look in a nested for loop (inside deepest loop). I personally agree with what many others have said, in that comprehension order is not intuitive as is. I still page fault about it after many years of using. Is there a way to move the expression bit to the end in a backcompat way? It might be a completely different syntax though (I think both colons and semicolons were suggested). FWIW, Erlang/Elixir (sorry after 6 years python this is what I do now!) does it the same way as python:
[{X, Y} || X <- [1,2,3], Y <- [a,b]]. [{1,a},{1,b},{2,a},{2,b},{3,a},{3,b}]
Here X is the outer loop. I think the confusion stems from doing it both ways at the same time. We retain the for loop order but then hoist the expression to the top. Ideally we'd either not do that, or reverse the for loop order.
participants (2)
-
C Anthony Risinger
-
Greg Ewing