Python 3.8+Numbers & mathfunction
math.dist
geometry
The Euclidean distance between two points (sequences of coordinates).
Syntax
math.dist(...)
Added in 3.8; the points are any sequences of coordinates of any dimension, but their lengths must match or you get ValueError. Internally it works like hypot: no overflow on the intermediate squares and less rounding error than a hand-rolled square root of a sum of squares. If you only compare distances with each other, comparing sums of squares without the root is cheaper.
Examples
import math
print(math.dist((0, 0), (3, 4))) # → 5.0
print(math.dist([1, 2, 3], [4, 6, 3])) # → 5.0
points = [(1, 1), (5, 5), (2, 3)]
print(min(points, key=lambda p: math.dist((0, 0), p))) # → (1, 1)
print(math.dist((0, 0), (1, 1, 1))) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy