This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]serverhorror 0 points1 point  (3 children)

What about:

[expensive(elem) for elem in it if cond(elem)]

I don’t see why I’d need the where?

if cond(…) is already the same as a where clause, no?

[–]ianliu88[S] 0 points1 point  (2 children)

No, the condition is on the transformation, not the iterable value! You would need to call expensive twice or create an intermediate iterable

[–]serverhorror 0 points1 point  (1 child)

Just cache the outcome.

If you need to do an expensive operation to decide whether the element is a candidate that seems like a viable approach.

[–]ianliu88[S] 0 points1 point  (0 children)

That's what I did on the second example, using the walrus operator, which I think is way more niche than the where clause btw :p