0

For example, if I want values in list a to multiply with values of the same position in list B :

A = [1,2,3] B = [4,5,6] Then the desired calculations are: 1 multiplied by 4, 2 multiplied by 5 and 3 multiplied by6

  • If any answer here helped you, please consider [accepting it](https://meta.stackexchange.com/q/5234/179419). This indicates that the issue is resolved, and helps others find the correct answer more easily. – M-Chen-3 Dec 06 '20 at 21:55

4 Answers4

2

Iterate through two lists at a time using the zip function.

A = [1, 2, 3]
B = [4, 5, 6]
C = []

for a, b in zip(A, B):
    C.append(a*b)
    
print(C)
# Prints [4, 10, 18]
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
2

Using list comprehension (faster than for loop):

>>> res_list = [ls1[i] * ls2[i] for i in range(len(ls1))] 

OR

Numpy (fastest method) :

>>> import numpy as np
>>> ls1 = np.array([1,2,3,4])
>>> ls2 = np.array([2,1,6,5])
>>> res = ls1 * ls2
>>> print(res)
array([2,3,18,20])

OR

for loop (slowest but easily readable) :

res= []
for i in range(len(a)):
     res.append(ls1[i]*ls2[i]) 
print(res)

Edit: Kindly check this speed performance graph from freeCodeCamp

enter image description here

Yash Makan
  • 706
  • 1
  • 5
  • 17
  • "Using list comprehension (faster than for loop)" list comprehensions are only marginally more performant than a loop, and as the work you do on each iteration increases that performance disappeared, list comprehensions aren't really for performance but for readability – juanpa.arrivillaga Dec 05 '20 at 18:42
  • @juanpa.arrivillaga Acc. to the graph using listcomp makes the code run more than 50% faster than with a for loop – Yash Makan Dec 05 '20 at 18:47
  • Right, because *multiplication* is a very fast thing. The optimizations for a list-comprehension are basic, avoiding the method resolution of `resulting_list.append` and a small stack-based bytecode optimization. You can even "cache" the method resolution by using `append = resulting_list.append` and using `append(whatever)` in a loop. Just that is enough to make the difference almost disappears. And again, as the work you do in the list comprehension increases, it becomes practically no difference. – juanpa.arrivillaga Dec 05 '20 at 19:03
  • For example, for `timeit.timeit('comp(A,B)', 'from __main__ import A, B, comp, loop', number=10_000)` I'm getting `12.15388411699999`, for `timeit.timeit('loop(A,B)', 'from __main__ import A, B, comp, loop', number=10_000)` with the "method caching" trick I mentioned, I'm getting `15.503835907000052` where `A = [1, 2, 3] * 10_000` and `B = [4, 5, 6] * 10_000` – juanpa.arrivillaga Dec 05 '20 at 19:03
  • That is a pretty terrible article, btw. – juanpa.arrivillaga Dec 05 '20 at 19:06
1

If you're looking for a one-liner, you can use list comprehension for this:

C = [x*y for x, y in zip(A, B)]

References:

Diggy.
  • 6,744
  • 3
  • 19
  • 38
1

Here are two ways to do it:

The more "Pythonic" way to do it is list comprehension:

A = [1, 2, 3]
B = [4, 5, 6]
C = [a * b for a, b in zip(A, B)] # [4, 10, 18]

Another way is to iterate on both of the lists with the function zip():

A = [1, 2, 3]
B = [4, 5, 6]
C = []
for a,b in zip(A, B):
    result = a * b
    C.append(result)
# C = [4, 10, 18]

Good Luck!

Raz K
  • 167
  • 8