>>> 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]
Answer from jamylak on Stack Overflow>>> 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]
Python: filter(None, [list of bools]) behavior - Stack Overflow
Is there a way to filter out none results from re.search?
python - Why isn't it filter(not None, iterable)? - Stack Overflow
Is there any downside of using if x / if not x versus if x is None / if x is not None ?
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.
Often, I will get a ('None', 'foo', 'None') Esque result from re.Search. From here, I'll filter out the results using match.Groups() Mapped to lambda x: x != None.
I'm wondering if there is an implicit way to cause this filtering to happen, so that I don't have to always go through this check.
Thanks!
For python3 you don't need to use a lambda, None.__ne__ will only filter None:
In [1]: list(filter(None.__ne__, [None, 0, 1, 2]))
Out[1]: [0, 1, 2]
Since the definition of
filter()is to return a list of elements in which the function returns True
But not None is a not function, and so not callable. None is used as shorthand for the identity predicate (not just function), which in this case can be lambda x: bool(x) or simply bool. So instead of what you propose, you would do:
filter(lambda x: x is not None, iterable)
More so, the None shorthand is a generalization that applies to some other builtin functions in python: map, itertools.ifilter, itertools.imap, itertools.ifilterfalse among others.
A pythonic equivalent of filter (an iterator version from the docs) will look:
def filter(predicate, iterable):
# filter(lambda x: x%2, range(10)) --> 1 3 5 7 9
if predicate is None:
predicate = bool # evaluates truth value of the object
for x in iterable:
if predicate(x):
yield x