Sum a list which contains ‘None’ using Python

You can use filter function

>>> sum(filter(None, [1,2,3,None]))
6

Updated from comments

Typically filter usage is filter(func, iterable), but passing None as first argument is a special case, described in Python docs. Quoting:

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

Leave a Comment

error code: 521