3 ms·
Python list comprehensions (at least IME) are not that complicated or incomprehensible, at least up to two levels of nesting, with say one if clause, maybe even
by vram22 9y ago
Python list comprehensions (at least IME) are not that complicated or incomprehensible, at least up to two levels of nesting, with say one if clause, maybe even two. In fact, the language designers even took care (for the sake of comprehensibility :) to ensure that the two fors (in a two-level nested listcomp) have the same meaning (aka order of priority) as a two-level nested for loop, i.e. outer for wraps inner for, in both cases. It's mentioned in the spec somewhere.
- lmm 9y ago> In fact, the language designers even took care (for the sake of comprehensibility :) to ensure that the semantics of the two fors (in a two-level nested listcomp) have the same meaning (aka order of priority) as a two-level nested for loop, i.e. outer for wraps inner for, in both cases. It's mentioned in the spec somewhere. Yeah, I think that was a huge mistake, because the most common case is one-level and if you have one level the syntax is just the reverse of a regular for, but in a two-level comprehension (the rare case) you have to remember that it's not a reversal, it's a cyclic permutation. i.e. most of the examples you see look like this, and these two are equivalent: [y for i in x] for i in x: y and then occasionally you see a comprehension that looks like: [z for j in y for i in x] so you'd expect this to be equivalent to for i in x: for j in y: z but it isn't, it's equivalent to for j in y: for i in x: z which is really hard to remember.
- vram22 9y agoHadn't thought of it that way myself, I may have just thought that the way (order) it works is the same as the way it is actually designed to work, and then later looking at the docs confirmed that, but yes, yours is a valid point of view.