Efficiently filtering out ‘None’ items in a list comprehension in which a function is called

Add an if into your comprehension like:

l = [y for y in (f(x) for x in [1,2,3,4]) if y is not None]

By placing a Generator Expression inside the list comprehension you will only need to evaluate the function once. In addition the generator expression is a generator so takes no extra intermediate storage.

Python 3.8+

As of Python 3.8 you can use an Assignment Expression (:=) (AKA: Named Expressions or the Walrus Operator) to avoid multiple evaluations of f() like:

l = [y for x in [1,2,3,4] if (y := f(x)) is not None]

Leave a Comment

techhipbettruvabetnorabahisbahis forumu