Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python filter() function with None as filter argument

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.

like image 409
bontha vijetha Avatar asked Oct 20 '25 02:10

bontha vijetha


1 Answers

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.

like image 141
jpp Avatar answered Oct 22 '25 18:10

jpp