Python math.dist() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.8+

Definition

math.dist(p, q) returns the Euclidean distance between two points p and q, each given as a sequence or iterable of coordinates.

Added in Python 3.8, it works in any number of dimensions (2D, 3D, or higher) and uses math.hypot() internally so differences do not needlessly overflow.

math.dist() is a function in Python's standard math module: import it with import math and call math.dist(...), or import the name directly with from math import dist. Added in Python 3.8.

Syntax

Import math and pass two equal-length coordinate sequences (Python 3.8+).

python
import math

d = math.dist(point_a, point_b)
CallWhat it does
math.dist((0, 0), (3, 4))Returns 5.0.
math.dist([1, 2, 3], [3, 5, 9])Returns 7.0 (3D distance).
math.dist([], [])Returns 0.0.
math.dist([1, 2], [1, 2, 3])Raises ValueError: dimension mismatch.

Both p and q must have the exact same length; use math.hypot() to measure distance from the origin.

Function signatures
python
math.dist(p, q, /)
# added in Python 3.8; p and q must have the same length

Parameters

ParameterDescription
pRequired iterable of numeric coordinates for the first point.
qRequired iterable of numeric coordinates for the second point, matching length of p.

Return Value

Return ValueDescription
floatEuclidean distance sqrt(sum((px - qx) ** 2)).

Python Examples

Example 1 — Find the Nearest Delivery Waypoint on a 2D Grid

Measure grid distances and use min() with a key to pick the closest station.

python
import math

drone = (2, 3)
hubs = {"North": (5, 7), "Dock": (3, 4), "East": (10, 3)}

for name, pos in hubs.items():
    print(f"{name:>5}: {math.dist(drone, pos):.2f}")

nearest = min(hubs, key=lambda name: math.dist(drone, hubs[name]))
print("nearest:", nearest)

How It Works

  1. Origin to hub. Offset from (2, 3) to (5, 7) is (3, 4), giving exact distance 5.00.
  2. Diagonal unit hop. Distance to (3, 4) is sqrt(2), formatted to 1.41.
  3. Minimum key search. Passing math.dist inside min(..., key=...) selects Dock without temporary lists.

Example 2 — Compare 3D Coordinates and 4D Feature Vectors

Compute exact 3D triangle lengths and high-dimensional feature distances.

python
import math

p3 = (1, 2, 3)
q3 = (3, 5, 9)
print("3D distance:", math.dist(p3, q3))

vec_a = [0.2, 0.5, 0.1, 0.8]
vec_b = [0.5, 0.9, 0.1, 0.8]
print("4D distance:", round(math.dist(vec_a, vec_b), 4))

How It Works

  1. 3D integer triple. Coordinate gaps of 2, 3, and 6 square to 4 + 9 + 36 = 49, yielding exact 7.0.
  2. Any iterable dimension. Four-element lists differ by 0.3 and 0.4 in the first two slots, giving 0.5.
  3. Consistent float result. Every call returns a float regardless of whether inputs are lists or tuples.

Example 3 — Handle Dimension Mismatches and Empty Sequences

Catch ValueError when point lengths differ and inspect empty coordinate tuples.

python
import math

print("empty points:", math.dist((), ()))

try:
    math.dist([1, 2], [1, 2, 3])
except ValueError:
    print("points must have the same number of dimensions")

How It Works

  1. Zero-dimensional space. Two empty tuples produce distance 0.0.
  2. Strict length match. Comparing a 2D point to a 3D point raises ValueError instead of truncating like zip().
  3. Fixed error handler. Catching ValueError lets your program validate user-supplied coordinates cleanly.

Frequently Asked Questions

math.dist() was added in Python 3.8.
Yes. It accepts points of any equal dimension, from 0D up to arbitrary lengths.
math.dist() raises ValueError instead of silently ignoring extra coordinates.
Yes. Any iterable of numbers works, including lists, tuples, and generators.
math.dist(p, q) computes the norm of the difference vector (p - q) using the same overflow-safe scaling as math.hypot().

Next: math.erf()

Return the Gauss error function at x as a float.

erf() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful