Home > front end >  Pairwise multiplication of elements between two lists
Pairwise multiplication of elements between two lists

Time:04-06

I am trying to calculate the product of every element from one list multiplied by every element from another list. from each list multiplied by each other.

For example, in list1 I have 4, which needs to be multiplied by 3 in list2. Next, the 4 in list1 needs to be multiplied by 1 in list2. The pattern continues until I receive the output: [12,4,36,8,6,2,18,4,21,7,63,14]. I haven't been able to achieve this -- here is the code I have so far:

def multiply_lists(list1,list2):
    for i in range(0,len(list1)):
        products.append(list1[i]*list2[i])
    return products

list1 = [4,2,7]
list2 = [3,1,9,2]
products = []
print ('list1 = ',list1,';','list2 = ', list2) 
prod_list = multiply_lists(list1,list2)
print ('prod_list = ',prod_list)

CodePudding user response:

Use list comprehension like this

print([i * j for i in list1 for j in list2])

Output:

[12, 4, 36, 8, 6, 2, 18, 4, 21, 7, 63, 14]

CodePudding user response:

Here are two concise approaches.

The first uses itertools.product() and a list comprehension:

from itertools import product

[x * y for x, y in product(list1, list2)]

But, this problem is very well-suited for itertools.starmap(), which motivates the second approach. If you're unfamiliar with the function, it takes in two parameters:

  • A function that takes in two parameters. In this case, we use operator.mul, a version of the multiplication operator that we can pass into a function. Note that functions can be passed into other functions in Python because functions are first class.
  • An iterable of iterables of size two. In this case, it's our output from itertools.product().

For each element in our iterable, it unpacks the element and passes each element as a parameter into the function specified by the first parameter. This gives us:

from itertools import product, starmap
import operator

list(starmap(operator.mul, product(list1, list2)))

Both of these output:

[12, 4, 36, 8, 6, 2, 18, 4, 21, 7, 63, 14]

If you want to extend this approach to more than two iterables, you can do (as suggested by flakes):

from math import prod
list(map(prod, product(list1, list2, <specify more iterables>)))

Other answers have suggested using multiple for loops inside the comprehension. Note that some consider this approach to be poor style; the use of itertools.product() avoids this issue entirely.

If you have any questions, feel free to comment -- I'm more than happy to clarify any confusion. I realize that these solutions may not be the most beginner-friendly. At the very least, I hope that these approaches may be useful for future readers.

CodePudding user response:

You're close. What you really want is two for loops so you can compare each value in one list against all of the values in the second list. e.g.

def multiply_lists(list1, list2):
    for i in range(len(list1)):
        for j in range(len(list2)):
            products.append(list1[i] * list2[j])
    return products

You also don't need a range col, you can directly iterate over the items of each list:

def multiply_lists(list1, list2):
    for i in list1:
        for j in list2:
            products.append(i * j)
    return products

And you could also do this as a comprehension:

def multiply_lists(list1, list2):
    return [i * j for i in list1 for j in list2]

CodePudding user response:

You should use nested loops to multiply elements of 2 list or more list to multiply with each other.

Corrected Answer:

def multiply_lists(list1,list2):
    for i in list1:
        for j in list2:
            products.append(i*j)
    return products

list1 = [4,2,7]
list2 = [3,1,9,2]
products = []
print ('list1 = ',list1,';','list2 = ', list2) 
prod_list = multiply_lists(list1,list2)
print ('prod_list = ',prod_list)
  • Related