{"record":{"id":"3a78157848413810","repo":"TheAlgorithms/Python","slug":"both-points-must-have-the-same-dimension","errorCode":null,"errorMessage":"Both points must have the same dimension.","messagePattern":"Both points must have the same dimension\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/chebyshev_distance.py","lineNumber":18,"sourceCode":"def chebyshev_distance(point_a: list[float], point_b: list[float]) -> float:\n    \"\"\"\n    This function calculates the Chebyshev distance (also known as the\n    Chessboard distance) between two n-dimensional points represented as lists.\n\n    https://en.wikipedia.org/wiki/Chebyshev_distance\n\n    >>> chebyshev_distance([1.0, 1.0], [2.0, 2.0])\n    1.0\n    >>> chebyshev_distance([1.0, 1.0, 9.0], [2.0, 2.0, -5.2])\n    14.2\n    >>> chebyshev_distance([1.0], [2.0, 2.0])\n    Traceback (most recent call last):\n        ...\n    ValueError: Both points must have the same dimension.\n    \"\"\"\n    if len(point_a) != len(point_b):\n        raise ValueError(\"Both points must have the same dimension.\")\n\n    return max(abs(a - b) for a, b in zip(point_a, point_b))\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/chebyshev_distance.py#L1-L21","documentation":"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.","triggerScenarios":"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]).","commonSituations":"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).","solutions":["Inspect both inputs and make sure they are the same length before calling: len(a) == len(b).","Fix the upstream data source so all vectors share one dimensionality (consistent schema/feature list).","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."],"exampleFix":"# before\nchebyshev_distance([1.0], [2.0, 2.0])  # ValueError\n\n# after\np, q = [1.0, 0.0], [2.0, 2.0]\nassert len(p) == len(q)\nchebyshev_distance(p, q)","handlingStrategy":"type-guard","validationCode":"def same_dim(a, b):\n    return len(list(a)) == len(list(b))\n\nassert same_dim(point_a, point_b)","typeGuard":"def is_point_pair(a, b) -> bool:\n    return all(hasattr(p, '__len__') for p in (a, b)) and len(a) == len(b)","tryCatchPattern":"try:\n    d = chebyshev_distance(a, b)\nexcept ValueError as e:\n    if 'same dimension' in str(e):\n        raise ValueError(f'incompatible vectors: len {len(a)} vs len {len(b)}') from e\n    raise","preventionTips":["Normalize datasets so every row has the same number of features before any distance computations.","Add an assert len(a) == len(b) in test fixtures that generate random points."],"tags":["maths","validation","dimension-mismatch","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}