0
Can someone help me? I have a list = [[10,20], [30,40],[50,60]]
I have to make the sum of the list elements p.e: (10+20), (30+40), (50+60). Then I have to add the new result into the list above. Output should be: [[10,20,30], [30,40,70], [50,60,110]] How could I solve this?
16 Answers
+ 5
I think that above all you learning python, you should make all without utility functions... Try to use for loop, temporary list etc
+ 7
🍿 Nom nom nom... crunch crunch... 🍿 Don't mind me... I'm just enjoying the brilliant coaching by KrOW 😜
+ 7
KrOW Oh well... a few solutions were provided before Refik had a chance to solve this himself.
Refik Mušić ... Perhaps you can challenge yourself to solve this before studying the solutions already provided.
Виктор Нижурин Your solution is a really good example without using special functions. Try doing the same with list comprehensions.
Souvik Since you are using a special function, try refining with a single list object and use only a single loop.
Perhaps try doing this with attempt list comprehensions as well.
If any of you get stuck, take a look at the single for loop and the list comprehensions examples I just created.
https://code.sololearn.com/cndzu1G637qR/?ref=app
+ 5
You know loops? Loop on input array and for any item (which is another list) loop on this also, create a temp list where you put all subitems (items of any input items) calculating also the sum. At end of loop, put in this temporary list the sum, and append it to output list.
+ 5
Okay, you should solve it yourself, but I can provide some hints.
1. Iterate over the main list.
2. Use the sum() function to get sum of the inner lists.
3. Use the list.append() method to add the result at the end of each list.
If you get stuck, show your code, and I can help you further.
+ 5
Okay, have you studied for loops and the list.append method yet?
+ 5
KrOW Hopefully... Otherwise, what's the point of studying this?
BTW... I refined my code. Looks much more organized now. 😜
+ 4
David Carroll I hope only that Refik Mušić try to figure out itself how do his work before see proposed code solutions
+ 3
David Carroll thank you 😁😁😁... When and if i can, i try always to place the user such as itself get the solution to his problem because is the best way for learn 😉
+ 3
Possible solution:
inp_list=[[10,20],[30,40],[50,60]]
print inp_list
for elist in inp_list:
sum=0
for innerlist in elist:
sum+=innerlist
elist.append(sum)
print inp_list
+ 2
@Kishalaya Saha
To be honest: I´m a total beginner (3 weeks python). I tried the sum() function this way:
print (list[0], sum(list[0])..........
I don´t know how to use it for the inner lists. Thank you in advance!
+ 2
The right way is using a loop because (probably) who assigned this want make sure that you have understood it... Do you know how loop on list?
At example you know what do this code?
myList= [0,5,6,3]
for i in myList:
print(i)
+ 2
Refik Mušić Yes, you have to do a (slightly) more "advanced" code but as all codes, you have to start to easy.... Now, your program must edit an a list of list, adding at any sub-list, the sum of declared items. This can be solved in many ways, but try to resolve it using loop... First, you have to loop on main list for get the sublists, next you have to loop any sublist for get the sum, then append this value to original sublist... Try to do it with for loops
Hint: i referenced 2 loops and remember that you can use list.append for add an item to list
+ 1
@ Kishalaya Saha
Yes, I have already done it. However, it does not help me with this problem somehow. I tried this way:
list = [[10,20],[30,40],[50,60]]
list[0].append(30)
list[1].append(70)
list[2].append(110)
print (list)
But that´s not the right way, unfortunately.
+ 1
I know that part. But I think my task is a little bit more advanced - I can´t handle that because I have this underlist. Maybe for you it looks very easy. I rly dont know how to loop that.