Dot Product in Python without NumPy

Michael Minkoff picture Michael Minkoff · Feb 4, 2016 · Viewed 12.1k times · Source

Is there a way that you can preform a dot product of two lists that contain values without using NumPy or the Operation module in Python? So that the code is as simple as it could get?

For example:

V_1=[1,2,3]
V_2=[4,5,6]

Dot(V_1,V_2)

Answer: 32

Answer

timgeb picture timgeb · Feb 4, 2016

Without numpy, you can write yourself a function for the dot product which uses zip and sum.

>>> def dot(v1, v2):
...     return sum(x*y for x,y in zip(v1,v2))
... 
>>> dot([1,2,3], [4,5,6])
32