Ignore an element while building list in python Ignore an element while building list in python python python

Ignore an element while building list in python


We could create a "subquery".

[r for r in (f(char) for char in string) if r is not None]

If you allow all False values (0, False, None, etc.) to be ignored as well, filter could be used:

filter(None, (f(char) for char in string) )# or, using itertools.imap,filter(None, imap(f, string))