3 ms·
The one that always confuses me is that multiple "for"s in the same comprehension work the opposite way from how you expect: >>> [[(a, b) for a in [1, 2, 3
by lmm 13y ago
The one that always confuses me is that multiple "for"s in the same comprehension work the opposite way from how you expect:
>>> [[(a, b) for a in [1, 2, 3]] for b in [4, 5, 6]]
[[(1, 4), (2, 4), (3, 4)], [(1, 5), (2, 5), (3, 5)], [(1, 6), (2, 6), (3, 6)]]
>>> [(a, b) for a in [1, 2, 3] for b in [4, 5, 6]]
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
- sp332 13y agoThe second one makes more sense to me. My brain reads it as: for a in [1, 2, 3]: for b in [4, 5, 6]:
- lmm 13y agoI don't because the statement itself flips compared to for style, so it seems like the whole thing should flip. i.e. for XXXXX: for YYYYY: ZZZZZ it would kind-of make sense to me if this translated into a for comprehension as [ZZZZZ for YYYYY for XXXXX] but instead it translates to [ZZZZZ for XXXXX for YYYYY] which seems decidedly middle-endian.
- Ixiaus 13y agoIt makes sense to me; it is actually quite similar to summation notation.