3 ms·
I'm a big python user and I didn't realize this. It seems like the nesting order for list comprehensions is backwards? I would naturally read the line as state
by lmm 15y ago
I'm a big python user and I didn't realize this. It seems like the nesting order for list comprehensions is backwards? I would naturally read the line as
state = [st for s in [state for st in states[(s,letter)]]]
because that's the way the natural language brackets, but this line is obviously broken. I would expect the syntax for the for loop you write to look like "state = [st for st in states(s, letter) for s in state]", which I think reads very clearly and understandably. So can anyone explain why the nesting works the way it does?
- divtxt 15y agoI too expected the syntax list comprehensions to chained. Turns out that nesting is a feature: http://www.python.org/dev/peps/pep-0202/ http://www.python.org/dev/peps/pep-0202/ - The form [... for x... for y...] nests, with the last index varying fastest, just like nested for loops.
- ajanuary 15y agoMy background is with LINQ, which means I find it more natural to group it as: st {for s in state} {for st in states[(s,letter)]} But I agree, reading it as English your grouping makes more sense.
- evmar 15y agoThis got me too -- I thought the reason the code was "the strangest" was because the double-loop was nested the wrong way.