List problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

List problem

I want to make a new list which add old list's ith elements for example : old list = [1,2,3] , new list will be = [1,3,6]

12th Jun 2019, 12:55 PM
Aakash Gupta
Aakash Gupta - avatar
5 Answers
+ 4
from itertools import accumulate new_list = list(accumulate(old_list))
12th Jun 2019, 1:01 PM
HonFu
HonFu - avatar
+ 4
May be the code is more as i dont know much python . I hope u can understand this code. l=[1,2,3] m=[] s=[] for i in range(len(l)): for j in range(i+1): s.append(l[j]) m.append(sum(s)) s.clear() print(m)
12th Jun 2019, 3:13 PM
Nandini
+ 2
Aakash Gupta, have you actually tried my method? Because it does exactly what you say.
12th Jun 2019, 4:23 PM
HonFu
HonFu - avatar
+ 2
Ok Mr. HonFu .. i will try it today and thanks @HonFu and ?P.Nandini
13th Jun 2019, 3:50 AM
Aakash Gupta
Aakash Gupta - avatar
+ 1
If u have any doubts u can ask
12th Jun 2019, 3:14 PM
Nandini