How to Calculate the Dot Product of Two Lists in Python
-
Using the
sum()
andzip()
Function to Calculate the Dot Product in Python -
Calculate the Dot Product Using the
map()
andmul()
Function in Python -
Calculate the Dot Product Using
more_itertools.dotproduct
in Python - Calculate the Dot Product Using NumPy in Python
The dot product is a mathematical operation also known as the scalar product. A dot product is an algebraic expression that takes in a sequence of two equal lengths and returns a single number as a result.
Using the sum()
and zip()
Function to Calculate the Dot Product in Python
We can calculate the dot product of lists of equal length using the zip()
function and the sum()
function.
The zip
function returns a zip object by combining elements in a sequence of tuples from both iterables. On the other hand, the sum
function returns the sum of items in iterables such as lists.
Since the dot product mathematically involves a series of sum and products of elements from the sequence of numbers, we can calculate the dot product of two lists using the combinations of these two.
num1 = [2, 4, 6, 8, 10]
num2 = [10, 20, 30, 40, 50]
print(sum([i * j for (i, j) in zip(num1, num2)]))
Output:
1100
Calculate the Dot Product Using the map()
and mul()
Function in Python
The operator
module in Python provides a set of functions to import and use intrinsic operators in Python to perform various operations.
These operations include logical operations, sequence operations, mathematical operations, and object comparison. The mul()
function performs the element-wise multiplication of objects with a series of numbers such as data frames.
On the other hand, the map()
function is a built-in function that allows us to apply a certain function to all elements of an iterable. Finally, we would also use the sum()
function to calculate the sum of the products from the two lists of numerical values, as shown in the code below.
from operator import mul
num1 = [2, 4, 6, 8, 10]
num2 = [10, 20, 30, 40, 50]
print(sum(map(mul, num1, num2)))
Output:
1100
Calculate the Dot Product Using more_itertools.dotproduct
in Python
Python more_itertools
is a Python library that provides elegant functions for working with iterables in Python. Functions provided by the more_itertools
library allow us to group and select iterables among other operations such as windowing, combinatorics, and wrapping.
The more_itertools
library does not only provide solutions for complex iterations; it is also is more elegant and memory efficient. Using the more_itertools.product()
function, we can calculate the dot product of a sequence of numbers in a list as shown below.
import more_itertools as mit
num1 = [2, 4, 6, 8, 10]
num2 = [10, 20, 30, 40, 50]
print(mit.dotproduct(num1, num2))
Output:
1100
Calculate the Dot Product Using NumPy in Python
NumPy is a scientific Python package that allows us to work with multidimensional objects such as arrays and matrices.
NumPy is fast but more efficient since we can achieve much using very little code. We can seamlessly work with arrays without explicit indexing and looping through its vectorized code.
Below is a solution that uses a for loop and the combination of the product and addition operator to calculate the dot product of two lists.
num1 = [2, 4, 6, 8, 10]
num2 = [10, 20, 30, 40, 50]
dot_product = 0
for x, y in zip(num1, num2):
dot_product = dot_product + x * y
print("The dot product of the two lists is: ", dot_product)
Output:
The dot product of the two lists is: 1100
Although this solution calculates the dot product, NumPy offers a more elegant alternative without writing any loop.
Using the numpy.dot()
method, we can easily calculate the dot product of a sequence of numbers in two lists. This solution is precise and therefore not prone to errors and can be implemented in the code below.
import numpy as np
num1 = [2, 4, 6, 8, 10]
num2 = [10, 20, 30, 40, 50]
dot_product = np.dot(num1, num2)
print("The dot product of the two lists is: ", dot_product)
Output:
The dot product of the two lists is: 1100
Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.
LinkedInRelated Article - Python List
- How to Convert a Dictionary to a List in Python
- How to Remove All the Occurrences of an Element From a List in Python
- How to Remove Duplicates From List in Python
- How to Get the Average of a List in Python
- What Is the Difference Between List Methods Append and Extend
- How to Convert a List to String in Python