Multiplying two lists in Python
This tutorial will demonstrate various ways to perform multiplication of two list elements in Python. Suppose we have two lists of integers of the same dimension, and we want to multiply the elements in the first list with the elements at the same position in the second list to get a resulting list of the same dimension.
zip()
Multiply two lists
using the
The built-in tuple method in Python zip()
accepts one or more iterables and aggregates the iterables into a tuple. Lists like _and_ [1,2,3]
will [4,5,6]
become [(1, 4), (2, 5), (3, 6)]
_. Using map()
the _ method, we access both lists element-wise and use list comprehension to get the required list.
The following code example shows how to use zip()
with list comprehensions to reuse 1D and 2D lists.
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]]
numpy.multiply()
Multiply two lists
using the
NumPy
The method of
the Python library multiply()
takes two arrays/lists as input and returns an array/list after element-wise multiplication. This method is straightforward because we don't need to do any extra work for the two-dimensional multiplication, but the disadvantage of this method is that it NumPy
cannot be used without the library.
The following code example demonstrates how to use numpy.multiply()
the multiplication method to multiply 1D and 2D lists in Python.
- 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]]
map()
Multiply two lists
in Python using the
map
Functions take a function and one or more iterables as input, and return an iterable that applies the provided function to the input list.
We can use the multiplication function in Python to perform 1D and 2D element-wise multiplication of map()
two lists by passing map()
the two lists as arguments to the multiplication function. The following code example demonstrates how we can use map()
multiplication to perform multiplication of 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]]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
Sorting a list by another list in Python
Publish Date:2025/05/09 Views:148 Category:Python
-
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values in another list in this art
Normalizing a list of numbers in Python
Publish Date:2025/05/09 Views:134 Category:Python
-
Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn
How to create a list of a specific size in Python
Publish Date:2025/05/09 Views:195 Category:Python
-
Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel