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]
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]
Just combine your solutions:
[x for x in [fn(i) for i in lst] if x is not None]
The downside is that any None
s in the original list not produced by fn
will be removed as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With