Is it possible to add a where clause with list comprehension?
问题 Consider the following list comprehension [ (x,f(x)) for x in iterable if f(x) ] This filters the iterable based a condition f and returns the pairs of x,f(x) . The problem with this approach is f(x) is calculated twice. It would be great if we could write like [ (x,fx) for x in iterable if fx where fx = f(x) ] or [ (x,fx) for x in iterable if fx with f(x) as fx ] But in python we have to write using nested comprehensions to avoid duplicate call to f(x) and it makes the comprehension look