3 ms·
> ... 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 rh
by quietbritishjim 2y 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:
l = [
y.foo()
for x in my_list
if x.blah() > 7
for y in x.ys()
]
Is equivalent to this:
l = []
for x in my_list:
if x.blah() > 7:
for y in x.ys():
l.append(y.foo())
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).