TheAlgorithms/Python · error · Exception

Vector is empty

Error message

Vector is empty

What it means

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.

Source

Thrown at linear_algebra/src/lib.py:171

        self.__components[pos] = value

    def euclidean_length(self) -> float:
        """
        returns the euclidean length of the vector

        >>> Vector([2, 3, 4]).euclidean_length()
        5.385164807134504
        >>> Vector([1]).euclidean_length()
        1.0
        >>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length()
        9.539392014169456
        >>> Vector([]).euclidean_length()
        Traceback (most recent call last):
            ...
        Exception: Vector is empty
        """
        if len(self.__components) == 0:
            raise Exception("Vector is empty")
        squares = [c**2 for c in self.__components]
        return math.sqrt(sum(squares))

    def angle(self, other: Vector, deg: bool = False) -> float:
        """
        find angle between two Vector (self, Vector)

        >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]))
        1.4906464636572374
        >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True)
        85.40775111366095
        >>> Vector([3, 4, -1]).angle(Vector([2, -1]))
        Traceback (most recent call last):
            ...
        Exception: invalid operand!
        """
        num = self * other
        den = self.euclidean_length() * other.euclidean_length()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check len(v) == 0 before calling and return 0.0 (or skip) if the empty case is expected.
  2. Trace where the Vector was constructed and handle empty input there (early return, default value, or error).
  3. Guard filters: if not filtered: skip instead of building a Vector from them.
  4. Catch Exception narrowly around the call for defensive boundaries.

Example fix

// before
length = Vector([]).euclidean_length()  # Exception: Vector is empty

// after
v = Vector(components)
length = 0.0 if len(v) == 0 else v.euclidean_length()
Defensive patterns

Strategy: validation

Validate before calling

if len(v) == 0:
    return 0.0  # or: raise ValueError("input produced an empty vector")
norm = v.euclidean_length()

Type guard

from linear_algebra.src.lib import Vector

def is_nonempty_vector(obj) -> bool:
    return isinstance(obj, Vector) and len(obj) > 0

Try / catch

try:
    norm = v.euclidean_length()
except Exception as e:
    if "Vector is empty" in str(e):
        norm = 0.0  # choose a policy: default, skip, or re-raise with context
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: 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 []).

Related errors


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