11 ms·
From the article: result = [x for sublist in nested_list for x in sublist if x % 3 == 0] This explanation still confuses people initially. I suggest this
by gspetr 10y ago
From the article:
result = [x for sublist in nested_list for x in sublist if x % 3 == 0]
This explanation still confuses people initially. I suggest this one:
result = [a for b in c for a in b if a % 3 == 0]
Here it's more obvious that the outer loop arguments are to be reversed in the latter part of the nested comprehension.
However I would advise against any further nesting than this, you will not want to debug that if anything goes wrong with one of the inputs here, especially if you also do any conditional logic here in the comprehension.