I want to add two numbers to the list next to each other and return the final answer in a list using python. for example my_list = [1,2,3,5,6] And the final list will be [1,3,5,8,11] for the first digit I will consider the initial value as 0.
I tried this way to get the answer
list1 = [1,2,3,0,5,6]
first_value = 0
new_list = []
for i,each_value in enumerate(list1):
if i == 0:
sum = first_value + each_value
else:
sum = each_value + prev_val
new_list.append(sum)
#i = i+1
prev_val = each_value
print(new_list)
I have got the output that I want but I wanted to know can we write the same code with less number of lines either by using lambda or some python collection.