{"record":{"id":"3bcfb34895b12bbd","repo":"TheAlgorithms/Python","slug":"vector-is-empty","errorCode":null,"errorMessage":"Vector is empty","messagePattern":"Vector is empty","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"linear_algebra/src/lib.py","lineNumber":171,"sourceCode":"        self.__components[pos] = value\n\n    def euclidean_length(self) -> float:\n        \"\"\"\n        returns the euclidean length of the vector\n\n        >>> Vector([2, 3, 4]).euclidean_length()\n        5.385164807134504\n        >>> Vector([1]).euclidean_length()\n        1.0\n        >>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length()\n        9.539392014169456\n        >>> Vector([]).euclidean_length()\n        Traceback (most recent call last):\n            ...\n        Exception: Vector is empty\n        \"\"\"\n        if len(self.__components) == 0:\n            raise Exception(\"Vector is empty\")\n        squares = [c**2 for c in self.__components]\n        return math.sqrt(sum(squares))\n\n    def angle(self, other: Vector, deg: bool = False) -> float:\n        \"\"\"\n        find angle between two Vector (self, Vector)\n\n        >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]))\n        1.4906464636572374\n        >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True)\n        85.40775111366095\n        >>> Vector([3, 4, -1]).angle(Vector([2, -1]))\n        Traceback (most recent call last):\n            ...\n        Exception: invalid operand!\n        \"\"\"\n        num = self * other\n        den = self.euclidean_length() * other.euclidean_length()","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/linear_algebra/src/lib.py#L153-L189","documentation":"Raised by Vector.euclidean_length() in linear_algebra/src/lib.py:171 when the vector has zero components. The Euclidean norm sqrt(sum(c**2)) of an empty vector is mathematically defined as 0, but the implementation explicitly refuses empty input rather than returning 0, treating an empty vector as invalid state.","triggerScenarios":"Calling Vector([]).euclidean_length(), or calling it on a Vector built from an empty list, an exhausted generator, or a filtered sequence that matched nothing (Vector([x for x in data if x > 100]) with no matches).","commonSituations":"Data pipelines that filter rows before vectorizing — an empty filter result becomes an empty Vector; parsing an empty CSV/line into components; or a default-constructed Vector() with no arguments (components default to []).","solutions":["Check len(v) == 0 before calling and return 0.0 (or skip) if the empty case is expected.","Trace where the Vector was constructed and handle empty input there (early return, default value, or error).","Guard filters: if not filtered: skip instead of building a Vector from them.","Catch Exception narrowly around the call for defensive boundaries."],"exampleFix":"// before\nlength = Vector([]).euclidean_length()  # Exception: Vector is empty\n\n// after\nv = Vector(components)\nlength = 0.0 if len(v) == 0 else v.euclidean_length()","handlingStrategy":"validation","validationCode":"if len(v) == 0:\n    return 0.0  # or: raise ValueError(\"input produced an empty vector\")\nnorm = v.euclidean_length()","typeGuard":"from linear_algebra.src.lib import Vector\n\ndef is_nonempty_vector(obj) -> bool:\n    return isinstance(obj, Vector) and len(obj) > 0","tryCatchPattern":"try:\n    norm = v.euclidean_length()\nexcept Exception as e:\n    if \"Vector is empty\" in str(e):\n        norm = 0.0  # choose a policy: default, skip, or re-raise with context\n    else:\n        raise","preventionTips":["Check len(v) > 0 before computing norms on vectors built from filters/parsers.","Guard the construction site: skip empty inputs instead of creating empty Vector objects.","Remember Vector() with no arguments creates an empty vector — always pass the components explicitly.","Decide the empty-vector policy (0.0, skip, or error) once in a wrapper instead of scattering checks."],"tags":["linear-algebra","vector","empty-input","norm","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}