I will be stealing the form of the question there: List comprehension with condition
I have a simple list.
>>> a = [0, 1, 2]
I want to make a new list from it using a list comprehension.
>>> b = [afunction(x) for x in a]
>>> b
[12458, None, 34745]
Pretty simple, but what if I want to operate only over non-None elements? I can do this:
>>> b = [y for y in [afunction(x) for x in a] if y != None]
I would rather like to be able to do it with single list comprehension, in a single run. This, I believe iterates over the list again to filter out the None
s, and nobody really likes that.