{"record":{"id":"4ad11cb5f36abd0c","repo":"TheAlgorithms/Python","slug":"both-points-must-be-in-the-same-n-dimensional-spac","errorCode":null,"errorMessage":"Both points must be in the same n-dimensional space","messagePattern":"Both points must be in the same n-dimensional space","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/manhattan_distance.py","lineNumber":41,"sourceCode":"    ValueError: Both points must be in the same n-dimensional space\n    >>> manhattan_distance([1,\"one\"], [2, 2, 2])\n    Traceback (most recent call last):\n        ...\n    TypeError: Expected a list of numbers as input, found str\n    >>> manhattan_distance(1, [2, 2, 2])\n    Traceback (most recent call last):\n         ...\n    TypeError: Expected a list of numbers as input, found int\n    >>> manhattan_distance([1,1], \"not_a_list\")\n    Traceback (most recent call last):\n         ...\n    TypeError: Expected a list of numbers as input, found str\n    \"\"\"\n\n    _validate_point(point_a)\n    _validate_point(point_b)\n    if len(point_a) != len(point_b):\n        raise ValueError(\"Both points must be in the same n-dimensional space\")\n\n    return float(sum(abs(a - b) for a, b in zip(point_a, point_b)))\n\n\ndef _validate_point(point: list[float]) -> None:\n    \"\"\"\n    >>> _validate_point(None)\n    Traceback (most recent call last):\n         ...\n    ValueError: Missing an input\n    >>> _validate_point([1,\"one\"])\n    Traceback (most recent call last):\n         ...\n    TypeError: Expected a list of numbers as input, found str\n    >>> _validate_point(1)\n    Traceback (most recent call last):\n         ...\n    TypeError: Expected a list of numbers as input, found int","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/manhattan_distance.py#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the data so both vectors have the same dimensionality.","If dimensions legitimately differ, pad or project both vectors to a common dimension deliberately before calling.","Add an assert len(a) == len(b) in your own pipeline to catch the mismatch at the source."],"exampleFix":"# before\nmanhattan_distance([1, 2], [1, 2, 3])\n\n# after\n# align vectors to the same dimensions first\nmanhattan_distance([1, 2, 0], [1, 2, 3])","handlingStrategy":"validation","validationCode":"if len(point_a) != len(point_b):\n    raise ValueError(f'dimension mismatch: {len(point_a)} vs {len(point_b)}')","typeGuard":null,"tryCatchPattern":"try:\n    d = manhattan_distance(a, b)\nexcept ValueError as e:\n    if 'n-dimensional' in str(e):\n        raise ValueError(f'cannot compare {a!r} and {b!r}: different lengths')\n    raise","preventionTips":["Build both vectors from the same key list so lengths match by construction.","Assert equal length where vectors are created, not where they are consumed."],"tags":["math","valueerror","dimension-mismatch","distance-metrics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}