Input : list(filter(None,["abs"," ",123,"sdf",""]))
Output : ['abs', ' ', 123, 'sdf']
As of my understanding None means "no value" in python. but in the above case how the filter function is removing the empty string.
I am confused with None in python. Can any one explain what exactly None represents in the above command and how come it is matching with non-empty strings.
The documentation makes application clear:
If function is
None
, the identity function is assumed, that is, all elements of iterable that are false are removed.
So filter(None, it)
removes all Falsy values such as []
, {}
, 0
, False
, set()
, ''
, None
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With