Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension with a function as the output and the conditional

Given some function that can return None or another value and a list of values:

def fn(x):
    if x == 4:
        return None
    else return x

lst = [1, 2, 3, 4, 5, 6, 7]

I want to have a list of the outputs of fn() that don't return None

I have some code that works which looks like so:

output = []
for i in lst:
    result = fn(i)
    if result:
        output.append(result)

I can express this as a list comprehension like so:

output = [fn(i) for i in lst if fn(i)]

but it runs fn(i) for anything that doesn't return None twice which is not desirable if fn is an expensive function.

Is there any way to have have the nice pythonic comprehension without running the function twice?

a possible solution:

output = [fn(i) for i in lst]
output = [o for o in f if o]
like image 552
Eric Blum Avatar asked May 04 '17 16:05

Eric Blum


2 Answers

Your problem is that None is produced by the function, not the thing you are iterating over. Try

 output = [fi for fi in map(fn, lst) if fi is not None]
like image 114
chepner Avatar answered Oct 13 '22 10:10

chepner


Just combine your solutions:

[x for x in [fn(i) for i in lst] if x is not None]

The downside is that any Nones in the original list not produced by fn will be removed as well.

like image 29
Scott Hunter Avatar answered Oct 13 '22 10:10

Scott Hunter