
On Fri, Dec 24, 2021 at 10:22:29AM -0000, Stefan Pochmann wrote:
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 simpler still: [x + 1 for x in [1,2,3] if x % 2 != 0] Or we can use the mighty walrus: [y for x in [1, 2, 3] if (y:=x+1) % 2 == 0] (although y leaks out of the comprehension). Using a hypothetical pipeline syntax with an even more hypothetical arrow-lambda syntax: [1, 2, 3] | map(x=>x+1) | filter(a=>a%2) | list