Python nested list sum

I am trying to make a list of nested elements

for example, numbers=[1,3,5,6,[7,8]] should produce sum=30

I wrote the following code:

 def nested_sum(L): sum=0 for i in range(len(L)): if (len(L[i])>1): sum=sum+nested_sum(L[i]) else: sum=sum+L[i] return sum 

The above code throws the following error:

an object of type 'int' does not have len ()

I also tried len([L[i]]) , still not working.

Can anybody help? This is Python 3.3.

+7
source share
9 answers

You need to use isinstance to check if the item is a list or not. In addition, you may need to iterate over the actual list to simplify the task.

 def nested_sum(L): total = 0 # don't use `sum` as a variable name for i in L: if isinstance(i, list): # checks if `i` is a list total += nested_sum(i) else: total += i return total 
+25
source

It is generally considered a more pythonic type of duck , rather than an explicit type check. Something like this will accept any iterative, not just lists:

 def nested_sum(a) : total = 0 for item in a : try: total += item except TypeError: total += nested_sum(item) return total 
+5
source

Fast recursion using lambda to handle nested lists:

 rec = lambda x: sum(map(rec, x)) if isinstance(x, list) else x 

rec applied to the list will return the sum (recursively) by the value, will return the value.

 result = rec(a) 
+4
source

I would summarize a flattened list:

 def flatten(L): '''Flattens nested lists or tuples with non-string items''' for item in L: try: for i in flatten(item): yield i except TypeError: yield item >>> sum(flatten([1,3,5,6,[7,8]])) 30 
+3
source

One alternative solution with a list:

 >>> sum( sum(x) if isinstance(x, list) else x for x in L ) 30 

Edit: And for lists with more than two levels (thanks @Volatility):

 def nested_sum(L): return sum( nested_sum(x) if isinstance(x, list) else x for x in L ) 
+2
source

An example of using a filter, map, and recursion:

 def islist(x): return isinstance(x, list) def notlist(x): return not isinstance(x, list) def nested_sum(seq): return sum(filter(notlist, seq)) + map(nested_sum, filter(islist, seq)) 

And here is an example of using reduction and recursion

 from functools import reduce def nested_sum(seq): return reduce(lambda a,b: a+(nested_sum(b) if isinstance(b, list) else b), seq) 

An example of using plain old recursion:

 def nested_sum(seq): if isinstance(seq[0], list): head = nested_sum(seq[0]) else: head = seq[0] return head + nested_sum(seq[1:]) 

An example of using simulated recursion:

 def nested_sum(seq): stack = [] stack.append(seq) result = 0 while stack: item = stack.pop() if isinstance(item, list): for e in item: stack.append(e) else: result += item return result 

The adjustment for processing self-reference lists is left as an exercise for the reader.

+1
source

This code also works.

 def add_all(t): total = 0 for i in t: if type(i) == list: # check whether i is list or not total = total + add_all(i) else: total += i return total 
+1
source
 def nnl(nl): # non nested list function nn = [] for x in nl: if type(x) == type(5): nn.append(x) if type(x) == type([]): n = nnl(x) for y in n: nn.append(y) return sum(nn) print(nnl([[9, 4, 5], [3, 8,[5]], 6])) # output:[9,4,5,3,8,5,6] a = sum(nnl([[9, 4, 5], [3, 8,[5]], 6])) print (a) # output: 40 
0
source

A simple solution would be to use nested loops.

 def nested_sum(t): sum=0 for i in t: if isinstance(i, list): for j in i: sum +=j else: sum += i return sum 
0
source

All Articles