Basically my question is say you have an list containing ‘None’ how would you try retrieving the sum of the list. Below is an example I tried which doesn’t work and I get the error: TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'. Thanks

    1. def sumImport(self):
    2. my_list = [[1,2,3,None],[1,2,3],[1,1],[1,1,2,2]]
    3. k = sum(chain.from_iterable(my_list))
    4. return k

    [

    【Python】列表:sum求和含None的list - 图1

    ](https://stackoverflow.com/users/771848/alecxe)

    alecxe

    435k109 gold badges1000 silver badges1135 bronze badges

    asked Sep 1, 2012 at 17:33

    [

    【Python】列表:sum求和含None的list - 图2

    ](https://stackoverflow.com/users/1103714/sam)

    You can use filter function

    1. >>> sum(filter(None, [1,2,3,None]))
    2. 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.

    answered Sep 1, 2012 at 17:34

    [

    【Python】列表:sum求和含None的list - 图3

    ](https://stackoverflow.com/users/523217/alexey-kachayev)

    3

    Remove None (and zero) elements before summing by using filter:

    1. >>> k = sum(filter(None, chain.from_iterable(my_list)))
    2. >>> k
    3. 20

    To see why this works, see the documentation for filter:

    filter(function, iterable)

    Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. 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 [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.

    answered Sep 1, 2012 at 17:35

    [

    【Python】列表:sum求和含None的list - 图4

    ](https://stackoverflow.com/users/61974/mark-byers)

    Mark ByersMark Byers

    756k172 gold badges1533 silver badges1433 bronze badges

    1

    Another suggestion:

    1. from itertools import chain
    2. k = sum(x for x in chain.from_iterable(my_list) if x)

    answered Sep 1, 2012 at 17:41

    [

    【Python】列表:sum求和含None的list - 图5

    ](https://stackoverflow.com/users/20670/tim-pietzcker)

    Tim PietzckerTim Pietzcker

    310k56 gold badges481 silver badges542 bronze badges

    5

    Assuming you want to treat None as zero, a simple way is

    1. sum(x if x is not None else 0 for x in chain.from_iterable(my_list))

    answered Sep 1, 2012 at 17:34

    [

    【Python】列表:sum求和含None的list - 图6

    ](https://stackoverflow.com/users/1427416/brenbarn)

    BrenBarnBrenBarn

    224k33 gold badges383 silver badges368 bronze badges

    3

    Explicitly, this is equivalent to filter:

    1. k = sum([x for x in chain.from_iterable(my_list) if x])

    That saves me from remembering another function. :P

    answered Sep 1, 2012 at 18:23

    [

    【Python】列表:sum求和含None的list - 图7

    ](https://stackoverflow.com/users/515392/taro-sato)

    Taro SatoTaro Sato

    1,4141 gold badge15 silver badges19 bronze badges

    You always have the option of just writing the loop you want:

    1. k = 0
    2. for sublist in my_list:
    3. for val in sublist:
    4. if val is not None:
    5. k += val

    But it certainly doesn’t hurt to know about filter either.

    answered Sep 1, 2012 at 17:41

    [

    【Python】列表:sum求和含None的list - 图8

    ](https://stackoverflow.com/users/94977/jason-orendorff)

    Jason OrendorffJason Orendorff

    39.4k3 gold badges58 silver badges95 bronze badges

    Just using sum and map:

    1. sum(map(lambda x: x or 0, [1,2,3,None]))

    answered Oct 9, 2021 at 11:12

    [

    【Python】列表:sum求和含None的list - 图9

    ](https://stackoverflow.com/users/9210255/tigertv-ru)

    TigerTV.ruTigerTV.ru

    1,0142 gold badges14 silver badges32 bronze badges

    Not the answer you’re looking for? Browse other questions tagged python sum or ask your own question.


    https://stackoverflow.com/questions/12229902/sum-a-list-which-contains-none-using-python