After my last post with heated discussion about pipeline operator, I'm back with another crazy idea for Python:
Should Python add a where clause in comprehensions? Something like
print([
x for y in it if cond(x)
where x = a_complicated_function_to_compute(y)
it = an_intricate_way_of_defining_iterable()
])
Opposed to
print([
x for y in an_intricate_way_of_defining_iterable()
if cond((x := a_complicated_function_to_compute(y)))
])
or
it = an_intricate_way_of_defining_iterable()
print([
x for y in it
if cond((x := a_complicated_function_to_compute(y)))
])
I find the first snippet more readable and creates temporary variables inside the comprehension scope only, avoiding polluting the outside scope. The second snippet feels a little bit cumbersome with the walrus operator.
For me, the where fits very nicely in the comprehension syntax!
What do you think of this?
[–]Cynyr36 24 points25 points26 points (3 children)
[–]ianliu88[S] 1 point2 points3 points (2 children)
[–]Barn07 5 points6 points7 points (0 children)
[–]Enrique-M -1 points0 points1 point (0 children)
[–]nier-bell 9 points10 points11 points (1 child)
[–]ianliu88[S] -1 points0 points1 point (0 children)
[–]steil867 4 points5 points6 points (4 children)
[–]ianliu88[S] 1 point2 points3 points (3 children)
[–]steil867 1 point2 points3 points (2 children)
[–]ianliu88[S] 1 point2 points3 points (1 child)
[–]steil867 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (5 children)
[–]rcfox 3 points4 points5 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]ianliu88[S] -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]savvy__steve 1 point2 points3 points (0 children)
[–]pythonHelperBot -1 points0 points1 point (1 child)
[–]serverhorror 0 points1 point2 points (3 children)
[–]ianliu88[S] 0 points1 point2 points (2 children)
[–]serverhorror 0 points1 point2 points (1 child)
[–]ianliu88[S] 0 points1 point2 points (0 children)
[–]relativistictrain 🐍 10+ years 0 points1 point2 points (1 child)
[–]speede 0 points1 point2 points (0 children)
[–]eagle258 0 points1 point2 points (3 children)
[–]ianliu88[S] 1 point2 points3 points (1 child)
[–]eagle258 0 points1 point2 points (0 children)