Reputation: 33
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)
Upvotes: 3
Views: 1894
Reputation: 1
the builtin zip
function generates tuples of elements sharing the same index in any given quantity of lists.
you may then map
--another builtin function-- to efficiently apply the function supplied as its call's first argument to each tuple in a list.
combining the two --the behaviour of either you may replicate yourself, albeit resulting in lesser performance due to python's nature-- you may map
a lambda a,b: a*b
to each tuple generated by the zip
ping of your two lists.
essentially,
prod_list = list(map(lambda a,b: a*b,
zip(list1, list2)
))
note that you may very well explicitly define and name the anonymous lambda function that returns the product of its two arguments ; for instance, you may declare global static variables such as MUL
and then call map(MUL, zip(list1,list2))
.
additionally, you will likely want to cast the result of map
to a list, as python returns a class which is essentially an implementation detail instead of the same type as its input.
Upvotes: 0
Reputation: 19252
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:
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.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.
Upvotes: 2
Reputation: 1
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)
Upvotes: 0
Reputation: 1118
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]
Upvotes: 3
Reputation: 23644
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]
Upvotes: 1