What is the reason that the filter for None takes the "0" away from given list in python?

Viewed 35

I am new to programming. I am trying to fix a bug in python. I came across a situation where when I do something like below, zero is removed automatically out of all the list of numbers.

d = list(filter(None,[0,5,8,9]))
print(d)
# -> [5,8,9]

Can anyone help me in this case.

1 Answers

From the documentation:

If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression ... (item for item in iterable if item) if function is None.

And 0 is falsey.

Related