Reputation: 5
x = [5,10,15,20,25,30] y = [60,90,120,150,190,200]
I want to multiply 5x60 , 10x90 , 15x120 , 20x150 , 25x190 , 30x200 and store them in z=[] how can I do it ? thanks I have tried this but it multiplies each element with all the element in x with all element in y
***x = [5,10,15,20,25,30]
y = [60,90,120,150,190,200]
sumx= sum(x)
sumy=sum(y)
for i in x:
z=[i* j for j in y]
print(z)***
result for this
[300, 450, 600, 750, 950, 1000]
[600, 900, 1200, 1500, 1900, 2000]
[900, 1350, 1800, 2250, 2850, 3000]
[1200, 1800, 2400, 3000, 3800, 4000]
[1500, 2250, 3000, 3750, 4750, 5000]
[1800, 2700, 3600, 4500, 5700, 6000]
Upvotes: 0
Views: 45
Reputation: 1
Found this to work:
x = [5,10,15,20,25,30]
y = [60,90,120,150,190,200]
a = 0
z = []
for i in range(len(x)):
z.append(x[a] * y[a])
a += 1
print(z)
Got this as an output:
[300, 900, 1800, 3000, 4750, 6000]
Upvotes: -1
Reputation: 11330
If you're planning on doing a lot of arithmetic with arrays, you might want to consider using numpy.
import numpy as np
x = np.array([5,10,15,20,25,30])
y = np.array([60,90,120,150,190,200])
result = x * y
If this is a one-time thing only, then @Jarvis's solution is absolutely the right answer.
Upvotes: 1
Reputation: 8564
You can do this:
ans = [i*j for i, j in zip(x, y)]
gives
[300, 900, 1800, 3000, 4750, 6000]
Upvotes: 2