
Dec. 24, 2021
11:22 a.m.
Chris Angelico wrote:
Here's the equivalent as a list comprehension, which I think looks better than either of the above: [x + 1 for x in [1,2,3] if x % 2 == 0]
That's not equivalent. You produce [3] instead of [2, 4]. So you rather proved that the proposal does have merit, as it's apparently easy to get the list comprehension wrong. Actually equivalent list comprehension: [x for x in [1,2,3] for x in [x + 1] if x % 2 == 0] Or spread across lines: [x for x in [1,2,3] for x in [x + 1] if x % 2 == 0]