The map
function can be very useful here. Using map
we can apply any function to each element of an iterable.
Nội dung chính
- Multiply Two List in Python Using the zip[] Method
- Multiply Two Lists in Python Using the numpy.multiply[] Method
- Multiply Two Lists in Python Using the map[] Function
- Related Article - Python List
Python 3.x
>>> def my_mul[x,y]:
... return x*y
...
>>> a = [1,2,3,4]
>>> b = [2,3,4,5]
>>>
>>> list[map[my_mul,a,b]]
[2, 6, 12, 20]
>>>
Of course:
map[f, iterable]
is equivalent to
[f[x] for x in iterable]
So we can get our solution via:
>>> [my_mul[x,y] for x, y in zip[a,b]]
[2, 6, 12, 20]
>>>
In Python 2.x map[]
means: apply a function to each element of an iterable and construct a new list. In Python 3.x, map
construct iterators instead of lists.
Instead of my_mul
we could use
mul
operator
Python 2.7
>>>from operator import mul # import mul operator
>>>a = [1,2,3,4]
>>>b = [2,3,4,5]
>>>map[mul,a,b]
[2, 6, 12, 20]
>>>
Python 3.5+
>>> from operator import mul
>>> a = [1,2,3,4]
>>> b = [2,3,4,5]
>>> [*map[mul,a,b]]
[2, 6, 12, 20]
>>>
Please note that since map[]
constructs an iterator we use *
iterable unpacking operator to get a list. The unpacking approach is a bit faster then the list
constructor:
>>> list[map[mul,a,b]]
[2, 6, 12, 20]
>>>
- HowTo
- Python How-To's
- Multiply Two Lists in Python
Created: February-02, 2021 | Updated: February-07, 2021
- Multiply Two List in Python
Using the
zip[]
Method - Multiply Two Lists in Python Using the
numpy.multiply[]
Method - Multiply Two Lists in Python Using the
map[]
Function
This tutorial will demonstrate various methods to perform element-wise multiplication of two lists in Python. Suppose we have two lists of integers with the same dimensions and we want to multiply elements of the first list with the elements at the same position in the second list and get a resultant list with the same dimensions.
Multiply Two List in Python Using the zip[] Method
The built-in zip[]
method in Python takes one or more iterables and aggregates the iterables into a tuple. Like the lists [1,2,3]
and [4,5,6]
will become [[1, 4], [2, 5], [3, 6]]
. Using the map[]
method, we access both lists element-wise and get the required list using the list comprehension method.
The below code example shows how to multiple 1D and 2D lists using
zip[]
with the list comprehension.
list1 = [2,4,5,3,5,4]
list2 = [4,1,2,9,7,5]
product = [x*y for x,y in zip[list1,list2]]
print[product]
Output:
[8, 4, 10, 27, 35, 20]
Multiplication of 2D Lists:
list1 = [[2,4,5],[3,5,4]]
list2 = [[4,1,2],[9,7,5]]
product = [[0]*3]*2
for x in range[len[list1]]:
product[x] = [a*b for a,b in zip[list1[x],list2[x]]]
print[product]
Output:
[[8, 4, 10], [27, 35, 20]]
Multiply Two Lists in Python Using the numpy.multiply[] Method
The multiply[]
method of the NumPy
library in Python, takes two arrays/lists as input and returns an array/list after performing element-wise multiplication. This method is straightforward, as we do not
have to do any extra work for 2D multiplication, but the negative point of this method is that it can’t be used without the NumPy
library.
The below code examples demonstrates how to multiply 1D and 2D lists in Python using the numpy.multiply[]
method.
- 1D multiplication:
import numpy as np
list1 = [12,3,1,2,3,1]
list2 = [13,2,3,5,3,4]
product = np.multiply[list1,list2]
print[product]
Output:
[156 6 3 10 9 4]
- 2D multiplication:
import numpy as np
list1 = [[12,3,1],[2,3,1]]
list2 = [[13,2,3],[5,3,4]]
product = np.multiply[list1,list2]
print[product]
Output:
[[156 6 3]
[ 10 9 4]]
Multiply Two Lists in Python Using the map[] Function
The map
function takes a function and one or more iterables as input and returns an iterable applying the provided function on the input lists.
We can perform 1D and 2D element-wise multiplication of two lists in Python using the map[]
function by passing both lists as the arguments to the map[]
function. The below code examples demonstrate how we can use the map[]
to multiply two Python lists.
Example code for 1D multiplication:
list1 = [2,4,5,3,5,4]
list2 = [4,1,2,9,7,5]
product = list[map[lambda x,y: x*y ,list1,list2]]
print[product]
Output:
[8, 4, 10, 27, 35, 20]
Example code for 2D multiplication:
list1 = [[2,4,5],[3,5,4]]
list2 = [[4,1,2],[9,7,5]]
product = [[0]*3]*2
for x in range[len[list1]]:
product[x] = list[map[lambda a,b: a*b ,list1[x],list2[x]]]
print[product]
Output:
[[8, 4, 10], [27, 35, 20]]
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.