Jin
Jin

Reputation: 1223

sum of nested list in Python

I try to sum a list of nested elements

e.g, 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 gives following error:

object of type 'int' has no len()

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

Anyone can help? It is Python 3.3

Upvotes: 13

Views: 58388

Answers (17)

Rm4n
Rm4n

Reputation: 888

An alternative way for solving this problem can be this one-liner:

import ast

nested_list = [1,3,5,6,[7,8]]

res = sum(ast.literal_eval(str(nested_list).replace('[','').replace(']','')))

print(res)

Disclaimer: I don't think this is an efficient solution, it just works.

Upvotes: 0

KiamehrPropy
KiamehrPropy

Reputation: 1

This error shows up because 'L' is a integer Perhaps change it to some other data type.

Upvotes: 0

Farzin347
Farzin347

Reputation: 1

at first you should determine the type of the members of the list whether they are list or not before you sum the members .

def nested_sum(x):
sum = 0
for i in range(len(x)):
    if type(x[i]) == list:
        sum += nested_sum(x[i])
    else:
        sum+=x[i]
return sum

Upvotes: 0

Anil Kumar
Anil Kumar

Reputation: 565

def list_sum(L):
    return sum(list_sum(x) if isinstance(x, list) else x for x in L)

Upvotes: 0

Tausif Khan
Tausif Khan

Reputation: 1

    #nested sum

    l = [[1, 2], [3,5], [6,2], [4, 5, 6,9]] 

    def nested_sum(lst):

       sum = 0

       for i in lst:
          for j in i:
             sum = sum + j
       
       print(sum)
    
   
nested_sum(l) 

Upvotes: -2

Nandini  Ashok Tuptewar
Nandini Ashok Tuptewar

Reputation: 324

 def sum_nest_lst(lst):
         t=0
         for l in lst:
             if(type(l)==int):
                 t=t+l
             if(type(l)==list):
                 t=t+sum(l)
         print(t)

Upvotes: 1

Rejeeve
Rejeeve

Reputation: 1

L = [1, 2, 3, [4, 5, 6], 5, [7, 8, 9]]
total = 0 # assign any var 
for a in L: # assign index and start to iterate using if else
    if (isinstance(a, list)): # since its a list you are basically repeating the prev step
        for b in a:
            total += b
    else:
        total += a
print(total)

Upvotes: 0

Sumi
Sumi

Reputation: 1

def nested_sum(lists):
total = 0
for lst in lists:
    s = sum(lst)
    total += s
return total

Upvotes: -1

Sam Franklin
Sam Franklin

Reputation: 1

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

Upvotes: 0

Broseph
Broseph

Reputation: 1771

An example using filter and 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 using reduce 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 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 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

Adjustment for handling self-referential lists is left as an exercise for the reader.

Upvotes: 1

Alessandro Anderson
Alessandro Anderson

Reputation: 21

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

Upvotes: 0

yashraj
yashraj

Reputation: 31

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

Upvotes: 1

njzk2
njzk2

Reputation: 39406

A quick recursion that uses a lambda to handle the nested lists:

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

rec, applied on a list, will return the sum (recursively), on a value, return the value.

result = rec(a)

Upvotes: 3

dansalmo
dansalmo

Reputation: 11694

I would sum the 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

Upvotes: 6

Jaime
Jaime

Reputation: 67497

It is generally considered more pythonic to duck type, rather than explicit type checking. Something like this will take any iterable, not just lists:

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

Upvotes: 7

user2070336
user2070336

Reputation:

One alternative solution with list comprehension:

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

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

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

Upvotes: 9

Volatility
Volatility

Reputation: 32310

You need to use isinstance to check whether an element is a list or not. Also, you might want to iterate over the actual list, to make things simpler.

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

Upvotes: 33

Related Questions