Python: filter(None, [list of bools]) behavior

If you want to filter out None, use:

filter(lambda x: x is not None, [list of bools])

or

[x for x in [list of bools] if x is not None]

filter takes a function, not a value. filter(None, ...) is shorthand for filter(lambda x: x, ...) — it will filter out values that are false-y (emphasis mine):

filter(function, iterable)

Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)