TheAlgorithms/Python · error · ValueError

Both points must have the same dimension.

Error message

Both points must have the same dimension.

What it means

Raised by chebyshev_distance() in maths/chebyshev_distance.py when the two input points have different lengths. Chebyshev distance (the L-infinity metric, max coordinate difference) is only defined between points in the same vector space, so the function refuses mismatched dimensions before zipping. The check compares len(point_a) != len(point_b) and raises ValueError.

Source

Thrown at maths/chebyshev_distance.py:18

def chebyshev_distance(point_a: list[float], point_b: list[float]) -> float:
    """
    This function calculates the Chebyshev distance (also known as the
    Chessboard distance) between two n-dimensional points represented as lists.

    https://en.wikipedia.org/wiki/Chebyshev_distance

    >>> chebyshev_distance([1.0, 1.0], [2.0, 2.0])
    1.0
    >>> chebyshev_distance([1.0, 1.0, 9.0], [2.0, 2.0, -5.2])
    14.2
    >>> chebyshev_distance([1.0], [2.0, 2.0])
    Traceback (most recent call last):
        ...
    ValueError: Both points must have the same dimension.
    """
    if len(point_a) != len(point_b):
        raise ValueError("Both points must have the same dimension.")

    return max(abs(a - b) for a, b in zip(point_a, point_b))

View on GitHub (pinned to f5988cc097)

Solutions

  1. Inspect both inputs and make sure they are the same length before calling: len(a) == len(b).
  2. Fix the upstream data source so all vectors share one dimensionality (consistent schema/feature list).
  3. If comparing points of different spaces is genuinely needed, project or pad coordinates explicitly in your own code first — do not rely on the library to handle it.

Example fix

# before
chebyshev_distance([1.0], [2.0, 2.0])  # ValueError

# after
p, q = [1.0, 0.0], [2.0, 2.0]
assert len(p) == len(q)
chebyshev_distance(p, q)
Defensive patterns

Strategy: type-guard

Validate before calling

def same_dim(a, b):
    return len(list(a)) == len(list(b))

assert same_dim(point_a, point_b)

Type guard

def is_point_pair(a, b) -> bool:
    return all(hasattr(p, '__len__') for p in (a, b)) and len(a) == len(b)

Try / catch

try:
    d = chebyshev_distance(a, b)
except ValueError as e:
    if 'same dimension' in str(e):
        raise ValueError(f'incompatible vectors: len {len(a)} vs len {len(b)}') from e
    raise

Prevention

When it happens

Trigger: Calling chebyshev_distance([1.0], [2.0, 2.0]) or any call where the two list/tuple arguments differ in length, e.g. chebyshev_distance([0, 0], [1, 2, 3]).

Common situations: Passing rows of a ragged/nested dataset where rows have inconsistent column counts; comparing a 2D point against a 3D point after a coordinate-system change; off-by-one slicing that drops or adds a coordinate (point[:-1] vs point).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/3a78157848413810. Report an issue: GitHub.