TheAlgorithms/Python · error · ValueError

The Elements List is empty

Error message

The Elements List is empty

What it means

from_sequence.make_linked_list raises ValueError('The Elements List is empty') when elements_list is falsy — empty list, empty tuple, or None. The builder needs at least one element because it immediately uses elements_list[0] as the head Node.

Source

Thrown at data_structures/linked_list/from_sequence.py:48

        ...
    ValueError: The Elements List is empty
    >>> make_linked_list(())
    Traceback (most recent call last):
        ...
    ValueError: The Elements List is empty
    >>> make_linked_list([1])
    <1> ---> <END>
    >>> make_linked_list((1,))
    <1> ---> <END>
    >>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
    <1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
    >>> make_linked_list((1, 3, 5, 32, 44, 12, 43))
    <1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
    """

    # if elements_list is empty
    if not elements_list:
        raise ValueError("The Elements List is empty")

    # Set first element as Head
    head = Node(elements_list[0])
    current = head
    # Loop through elements from position 1
    for data in elements_list[1:]:
        current.next = Node(data)
        current = current.next
    return head

View on GitHub (pinned to f5988cc097)

Solutions

  1. Skip the call when the source is empty: 'if elements: head = make_linked_list(elements)'.
  2. Substitute a default element if an empty chain must still be built.
  3. Validate upstream producers so empty inputs are surfaced as their own error with context.

Example fix

# before
head = make_linked_list(data)  # ValueError when data == []
# after
head = make_linked_list(data) if data else None
Defensive patterns

Strategy: validation

Validate before calling

head = make_linked_list(elements) if elements else None

Type guard

def is_buildable(seq) -> bool:
    return bool(seq)  # rejects [], (), None

Try / catch

try:
    head = make_linked_list(seq)
except ValueError:
    head = None  # empty input

Prevention

When it happens

Trigger: make_linked_list([]), make_linked_list(()), or make_linked_list(None); feeding a generator-produced list that yielded nothing; passing a filtered result that happened to exclude all items.

Common situations: Building lists from query results or file lines where the source may legitimately be empty; defaults like make_linked_list(args.values or []) that collapse to empty; forgetting None is also rejected (falsy), not just [].

Related errors


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