10 ms·
Yes. The following is a lot more concise: a = [1, 2, 3, 4] print([v*v for v in reversed(a) if v*v % 2 == 0])
by rolux 2y ago
Yes. The following is a lot more concise:
a = [1, 2, 3, 4]
print([v*v for v in reversed(a) if v*v % 2 == 0])
- libria 2y agoI think the above is a good idea of what's wrong with python (and Go), because in your example the list comp is evaluated in what seems to be this order: FOURTH-> print([THIRD-> v*v for v in FIRST-> reversed(a) if SECOND-> v*v % 2 == 0]) Which is all over the place. I'd rather see: a = [1, 2, 3, 4] a = reversed(a) a = [v*v for v in a] a = [w for w in a if a % 2 == 0] print(a)
- alfons_foobar 2y agoThis. I often use generator expressions for the intermediate values (so I don't allocate a new list for each step), but I find this to be much more readable.