简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM