0
How to print this mixed nested list line by line?
n = [1,2,3,4,5,[1,2,3,4,5]]
9 Answers
+ 3
def fun(l):
for x in l:
if(type(x) is list):
print()
fun(x)
else:
print(x, end = " ")
n = [1,2,3,4,5,[1,2,3,4,5]]
fun(n)
+ 2
You only call nested_sum(x) but didn't add the return value from nested_sum function into variable <t>, this is why you got 15 as result.
n = [1,2,[1,2,3,4,5],[1,2,3,4,5],3,4,5]
def nested_sum(n):
t = 0
for x in n:
if(type(x) is list):
# add nested_sum return value to <t>
t += nested_sum(x)
else:
t += x
return t
print(nested_sum(n))
Hth, cmiiw
+ 1
Thanks a lot..
+ 1
If I am trying to add all numbers through this code works fine.. but if there are more list than 1 it is not working then why ? đ€
+ 1
Aakash Gupta ,
Can you share your code link? I can't say anything until I have a look at least ...
+ 1
đ€ thanks a lot.. give me some tips to improve my thinking.. I always stuck ..
+ 1
You're welcome, I'm not even sure what to say as tips, I've learned from making mistakes, and from seeing how other people do something, and ask them when there's something I don't understand (quite often happens).
Time will make you grow, as long as you keep on learning I guess đ
+ 1
I got it.. thanks âșïž
0
n = [1,2,[1,2,3,4,5],[1,2,3,4,5],3,4,5]
def nested_sum(n):
t = 0
for x in n:
if(type(x) is list):
nested_sum(x)
else:
t+=x
return t
print(nested_sum(n))
#Total is 45 but giving 15