TheAlgorithms/Python · error · ValueError

Both points must be in the same n-dimensional space

Error message

Both points must be in the same n-dimensional space

What it means

Raised by manhattan_distance() in maths/manhattan_distance.py when point_a and point_b have different lengths. The distance is computed element-wise via zip(point_a, point_b), so both points must describe vectors in the same n-dimensional space; mismatched lengths silently truncate with zip, so the library rejects them up front.

Source

Thrown at maths/manhattan_distance.py:41

    ValueError: Both points must be in the same n-dimensional space
    >>> manhattan_distance([1,"one"], [2, 2, 2])
    Traceback (most recent call last):
        ...
    TypeError: Expected a list of numbers as input, found str
    >>> manhattan_distance(1, [2, 2, 2])
    Traceback (most recent call last):
         ...
    TypeError: Expected a list of numbers as input, found int
    >>> manhattan_distance([1,1], "not_a_list")
    Traceback (most recent call last):
         ...
    TypeError: Expected a list of numbers as input, found str
    """

    _validate_point(point_a)
    _validate_point(point_b)
    if len(point_a) != len(point_b):
        raise ValueError("Both points must be in the same n-dimensional space")

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


def _validate_point(point: list[float]) -> None:
    """
    >>> _validate_point(None)
    Traceback (most recent call last):
         ...
    ValueError: Missing an input
    >>> _validate_point([1,"one"])
    Traceback (most recent call last):
         ...
    TypeError: Expected a list of numbers as input, found str
    >>> _validate_point(1)
    Traceback (most recent call last):
         ...
    TypeError: Expected a list of numbers as input, found int

View on GitHub (pinned to f5988cc097)

Solutions

  1. Fix the data so both vectors have the same dimensionality.
  2. If dimensions legitimately differ, pad or project both vectors to a common dimension deliberately before calling.
  3. Add an assert len(a) == len(b) in your own pipeline to catch the mismatch at the source.

Example fix

# before
manhattan_distance([1, 2], [1, 2, 3])

# after
# align vectors to the same dimensions first
manhattan_distance([1, 2, 0], [1, 2, 3])
Defensive patterns

Strategy: validation

Validate before calling

if len(point_a) != len(point_b):
    raise ValueError(f'dimension mismatch: {len(point_a)} vs {len(point_b)}')

Try / catch

try:
    d = manhattan_distance(a, b)
except ValueError as e:
    if 'n-dimensional' in str(e):
        raise ValueError(f'cannot compare {a!r} and {b!r}: different lengths')
    raise

Prevention

When it happens

Trigger: manhattan_distance([1,1], [1,1,1]), manhattan_distance([1], [1,2]), or any call where one list was built from a different feature set than the other.

Common situations: Comparing feature vectors built from different schemas, rows with missing values dropped independently, or appending to one list but not the other during data cleaning.

Related errors


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