As per definition, the None keyword is used to define a null value, or no value at all. But why does:
inputs = [3, 0, 1, 2, None]
print(list(filter(None, inputs)))
return this list [3,1,2] and not [3,0,1,2]?
As per definition, the None keyword is used to define a null value, or no value at all. But why does:
inputs = [3, 0, 1, 2, None]
print(list(filter(None, inputs)))
return this list [3,1,2] and not [3,0,1,2]?
 
    
     
    
    Per the filter docs:
If function is
None, the identity function is assumed, that is, all elements of iterable that are false are removed.
The identity function is basically:
def identity(something):
    return something
so filtering on this means that any value that evaluates false-y will be excluded from the output.
Then if you look at truth-value testing you can see that, as well as None, 0 evaluates false-y in Python:
Here are most of the built-in objects considered false:
- constants defined to be false:
NoneandFalse.- zero of any numeric type:
0,0.0,0j,Decimal(0),Fraction(0, 1)- ...
 
    
    The answer by @jonrsharpe explains why you get [3,1,2] instead of [3,0,1,2]. This is because 0 as well as None evaluates false-y in Python. 
But in case you want the code to perform as expected, try this:
inputs = [3, 0, 1, 2, None]
print(list(filter(lambda x: x is not None, inputs)))
This should return the output as [3,0,1,2]
