-2

I have two lists:

list1 = [1,2,3] list2 = [4,5,6]

I want to multiply each number in the first list with each number in the second list to get the result:

list3 = [4,5,6,8,10,12,12,15,18]

How do I do this?

  • 1
    What have you tried so far? You can read this: https://stackoverflow.com/questions/30587076/multiplying-every-element-of-one-array-by-every-element-of-another-array – 1x2x3x4x Oct 27 '21 at 09:43
  • I tried using zip but only multiplied each element in the first list with one corresponding element in the second list. – Edward Serfontein Oct 27 '21 at 09:49
  • https://www.kite.com/python/answers/how-to-multiply-two-lists-in-python – Edward Serfontein Oct 27 '21 at 09:49
  • Does this answer your question? [Multiplying every element of one array by every element of another array](https://stackoverflow.com/questions/30587076/multiplying-every-element-of-one-array-by-every-element-of-another-array) – 1x2x3x4x Oct 27 '21 at 09:51

1 Answers1

0

There could be a more pythonic way to do this but this gets the job done.

lst1 = [1,2,3]
lst2 = [4,5,6]
lst3 = []
for i in lst1:
    for j in lst2:
        lst3.append(i*j)
print(lst3)

Using list comprehension:

print([(l1*l2) for l1 in lst1 for l2 in lst2])
root_42
  • 91
  • 1
  • 6