Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to sum all the numbers in nested lists in Python

1 Answer

0 votes
def sum_the_nested(lst): 
    return sum(map(sum_the_nested, lst)) if hasattr(lst, '__iter__') else lst

lst = [[[[[[8]]]]], [1, 2, 3], [7, [9, 5], 0], [[90]], [[[[4, 5], 6], 19], 30]]


result = sum_the_nested(lst)

print(result)


#      6     +     8     +       21       +         64      +       90       =  189
# [[1, 2, 3], [[[[[8]]]]], [7, [9, 5], 0], [[[[4, 5], 6], 19], 30], [[90]]]



'''
run:

189

'''

 



answered Jun 24, 2024 by avibootz
...