I am new to python and I was just trying some list manipulations. I have three lists
A = [10,20,30,40,50,60]
B = [22,44,66,88,12,10]
C = [2,4,6,8,10,20]
I want to iterate over these three lists and for each value of C I want to add half of that value to the corresponding values of A and B. For example for the 1st iteration - half = 2/2= 1 So A = 10 + 1 and B = 22+1 So the final lists should look something like this
A = [11,22,33,44,55,70]
B = [23,46,69,92,17,20]
C = [2,4,6,8,10,20]
As long as the lists all have the same lengths, you can iterate with enumerate()
function:
for i, n in enumerate(C):
A[i] += n/2
B[i] += n/2
>>> A
[11, 22, 33, 44, 55, 70]
>>> B
[23, 46, 69, 92, 17, 20]
>>> A, B = zip(*((a + c/2, b + c/2) for a, b, c in zip(A, B, C)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With