Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a list comprehension that calls a function which potentially returns None.

>>> f = lambda x: x if x < 3 else None >>> l = [f(x) for x in [1,2,3,4]] [1, 2, None, None] 

I'd like to have the list comprehension as above, without the 'None' entries.

What's a more efficient way to do the following, without creating additional overhead, and while retaining the efficiency of the list comprehension?

>>> filter(None, [f(x) for x in [1,2,3,4]]) [1, 2] 
like image 461
scrap_metal Avatar asked Feb 04 '18 15:02

scrap_metal


People also ask

How do I filter none type in Python?

The easiest way to remove none from list in Python is by using the list filter() method. The list filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.

How do I remove all none values from a list in Python?

The Python filter() function is the most concise and readable way to perform this particular task. It checks for any None value in list and removes them and form a filtered list without the None values.

Why is it called a list comprehension?

Because it's a very comprehensive way to describe a sequence (a set in math and other languages, and a list/sequence in Python).

Which is faster filter or list comprehension?

1 Answer. Actually, list comprehension is much clearer and faster than filter+lambda, but you can use whichever you find easier.


1 Answers

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] 
like image 120
Stephen Rauch Avatar answered Oct 21 '22 13:10

Stephen Rauch