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
def sumImport(self):
my_list = [[1,2,3,None],[1,2,3],[1,1],[1,1,2,2]]
k = sum(chain.from_iterable(my_list))
return k
[
](https://stackoverflow.com/users/771848/alecxe)
435k109 gold badges1000 silver badges1135 bronze badges
asked Sep 1, 2012 at 17:33
[
](https://stackoverflow.com/users/1103714/sam)
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.
answered Sep 1, 2012 at 17:34
[
](https://stackoverflow.com/users/523217/alexey-kachayev)
3
Remove None
(and zero) elements before summing by using filter
:
>>> k = sum(filter(None, chain.from_iterable(my_list)))
>>> k
20
To see why this works, see the documentation for filter
:
filter(function, iterable)
Construct a list from those elements of
iterable
for whichfunction
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 isNone
, 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 notNone
and[item for item in iterable if item]
if function isNone
.
answered Sep 1, 2012 at 17:35
[
](https://stackoverflow.com/users/61974/mark-byers)
Mark ByersMark Byers
756k172 gold badges1533 silver badges1433 bronze badges
1
Another suggestion:
from itertools import chain
k = sum(x for x in chain.from_iterable(my_list) if x)
answered Sep 1, 2012 at 17:41
[
](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
sum(x if x is not None else 0 for x in chain.from_iterable(my_list))
answered Sep 1, 2012 at 17:34
[
](https://stackoverflow.com/users/1427416/brenbarn)
BrenBarnBrenBarn
224k33 gold badges383 silver badges368 bronze badges
3
Explicitly, this is equivalent to filter:
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
[
](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:
k = 0
for sublist in my_list:
for val in sublist:
if val is not None:
k += val
But it certainly doesn’t hurt to know about filter
either.
answered Sep 1, 2012 at 17:41
[
](https://stackoverflow.com/users/94977/jason-orendorff)
Jason OrendorffJason Orendorff
39.4k3 gold badges58 silver badges95 bronze badges
Just using sum and map:
sum(map(lambda x: x or 0, [1,2,3,None]))
answered Oct 9, 2021 at 11:12
[
](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