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):
Answer from agf on Stack Overflow
filter(function, iterable)Construct a list from those elements of
iterablefor whichfunctionreturns true.iterablemay be either a sequence, a container which supports iteration, or an iterator. Ifiterableis a string or a tuple, the result also has that type; otherwise it is always a list. If function isNone, the identity function is assumed, that is, all elements ofiterablethat are false are removed.Note that
filter(function, iterable)is equivalent to[item for item in iterable if function(item)]if function is notNoneand[item for item in iterable if item]if function isNone.
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
iterablefor whichfunctionreturns true.iterablemay be either a sequence, a container which supports iteration, or an iterator. Ifiterableis a string or a tuple, the result also has that type; otherwise it is always a list. If function isNone, the identity function is assumed, that is, all elements ofiterablethat are false are removed.Note that
filter(function, iterable)is equivalent to[item for item in iterable if function(item)]if function is notNoneand[item for item in iterable if item]if function isNone.
For python3 you can use None.__ne__ to only remove None, just filtering using None will remove any falsey values like [], {} 0 etc.. :
filter(None.__ne__, check(low, high, to_match))
for python2 you would need to add a lambda checking each element is not None:
filter(lambda x: x is not None,....)
If you are using python2 the stick to a list comp:
[ele for ele in check(low, high, match) if ele is not None]
Any performance gain of using filter will be offset by the lambda calls so it will actually end up slower.
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]
Just for fun, here's how you can adapt filter to do this without using a lambda, (I wouldn't recommend this code - it's just for scientific purposes)
>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]
A list comprehension is likely the cleanest way:
>>> L = [0, 23, 234, 89, None, 0, 35, 9
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]
There is also a functional programming approach but it is more involved:
>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]