list elements sum of one list first element to last element of second list

2024/11/18 16:20:21

How to sum first element of one list to last element of second list if both list contains same amount of elements in python.

Source code (should be kind of like this):

def update(l1,l2,l3):for i in range(len(l1)):l3.append(l1[i] + l2[-i]) #i know -i is not correct way to start indexing in reversel1=[1,2,3,4,5]
l2=[6,7,8,9,10]
l3=[]
update(l1,l2,l3)
print(l1)
print(l2)
print(l3)

It would mean a lot if someone could help me with this problem.

Answer

If you subtract the index from the length of the list, you add the first item and the last item consecutively.

# The expression l2[len(l2)-i-1] iterates from  the last element to the first element.
def update(l1,l2,l3):for i in range(len(l1)):l3.append(l1[i] + l2[len(l2) - i - 1])l1=[1,2,3,4,5]
l2=[6,7,8,9,10]
l3=[]
update(l1,l2,l3)
print(l1, l2, l3)

Output:

[1, 2, 3, 4, 5] [6, 7, 8, 9, 10] [11, 11, 11, 11, 11]
https://en.xdnf.cn/q/120053.html

Related Q&A

Append float data at the end of each line in a text file

I have a .txt file and Im trying to add float number at the end of each line with incremented value than the last line. This is what I have in the .txt file:>520.980000 172.900000 357.440000 >320…

Change and Sort list in list in specific order in python

Hello guys I have this type of data in list in list array = [ ["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AA…

How to interact with the reCAPTCHA Solve the challenge button using Selenium and Python

Im trying to interact with the recaptcha Solve the challenge button on image verification popup using Selenium and Python. The xpath looks correct in dev tools but using Selenium unable to interact wit…

How to convert an adjacency matrix to an adjacency list with python?

I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.],[ 15., 0., 9., 11., 0., 9.],[ 0., 9., 0., 0., 12., 7.],[ 7., 11., 0., 0., 8., 14.],[ 10., 0., 12., …

how to plot a box plot of a column of a data frame in two groups in matplotlib

I have the following data frame:value total_spend margin1 29493.14 10203.371 27551.22 19003.072 22881.26 15142.681 15254.77 12337.221 11873.95 9424.231 11382.89 8767.83…

Text manipulation to form an equation

a=0.77 ,b=0.2 ,c=0.20, d=0.79 ,z=(c+d), e=(z*a) ,output=(z+e) I have a text file like above. I need a parser logic that will throw an equation like output=(0.20+0.79)+((0.20+0.79)*a) what are some effi…

Python If statement and logical operator issue [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.Im trying to create a small python program to emulate rolling a…

How do I replace a specific string in a 2d array?

Im making a program that Identifies if a blank tile exists or not. I already have a code in my 2d array which is arr2 = [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0…

from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11

I actually working on a face recognition project but getting an error such as: from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11Please help Ill appreciate any bits of hel…

Sudoku with user input

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function. I ha…