{"record":{"id":"e5f1ab1c5b21059c","repo":"TheAlgorithms/Python","slug":"both-points-must-have-the-same-dimension-e5f1ab","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/minkowski_distance.py","lineNumber":37,"sourceCode":"    >>> minkowski_distance([1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], 2)\n    8.0\n    >>> import numpy as np\n    >>> bool(np.isclose(5.0, minkowski_distance([5.0], [0.0], 3)))\n    True\n    >>> minkowski_distance([1.0], [2.0], -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The order must be greater than or equal to 1.\n    >>> minkowski_distance([1.0], [1.0, 2.0], 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Both points must have the same dimension.\n    \"\"\"\n    if order < 1:\n        raise ValueError(\"The order must be greater than or equal to 1.\")\n\n    if len(point_a) != len(point_b):\n        raise ValueError(\"Both points must have the same dimension.\")\n\n    return sum(abs(a - b) ** order for a, b in zip(point_a, point_b)) ** (1 / order)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":19,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/minkowski_distance.py#L19-L46","documentation":"Raised by minkowski_distance() in maths/minkowski_distance.py when point_a and point_b have different lengths. The distance is computed by zipping the two points term by term, which would silently truncate on length mismatch, so the function raises ValueError to enforce equal dimensionality.","triggerScenarios":"minkowski_distance([1.0], [1.0, 2.0], 1) as in the doctest, or comparing any vectors of unequal length such as ([1,2,3], [1,2]).","commonSituations":"Feature vectors from different feature sets or schema versions, coordinates where one side carries an extra attribute, or lists built by independent filters.","solutions":["Ensure both points have the same number of components before calling.","Align vectors on a shared feature/column list, then extract values in the same order.","Assert equal length at the point of construction so the bug surfaces near its cause."],"exampleFix":"# before\nminkowski_distance([1.0], [1.0, 2.0], 1)\n\n# after\nminkowski_distance([1.0, 0.0], [1.0, 2.0], 1)  # aligned dimensions","handlingStrategy":"validation","validationCode":"if len(point_a) != len(point_b):\n    raise ValueError(f'points differ in dimension: {len(point_a)} vs {len(point_b)}')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Zip points over a shared index/key list before comparing.","Add dimension assertions in feature-extraction code."],"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"}