TheAlgorithms/Python · error · Exception

The Elements List is empty

Error message

The Elements List is empty

What it means

print_reverse.make_linked_list raises a generic Exception('The Elements List is empty') for falsy input, same contract as from_sequence but a different exception type (Exception vs ValueError). The returned LinkedList feeds in_reverse, which itself handles the truly empty list fine (returns '').

Source

Thrown at data_structures/linked_list/print_reverse.py:103

            self.append(item)


def make_linked_list(elements_list: Iterable[int]) -> LinkedList:
    """Creates a Linked List from the elements of the given sequence
    (list/tuple) and returns the head of the Linked List.
    >>> make_linked_list([])
    Traceback (most recent call last):
        ...
    Exception: The Elements List is empty
    >>> make_linked_list([7])
    7
    >>> make_linked_list(['abc'])
    abc
    >>> make_linked_list([7, 25])
    7 -> 25
    """
    if not elements_list:
        raise Exception("The Elements List is empty")

    linked_list = LinkedList()
    linked_list.extend(elements_list)
    return linked_list


def in_reverse(linked_list: LinkedList) -> str:
    """Prints the elements of the given Linked List in reverse order
    >>> in_reverse(LinkedList())
    ''
    >>> in_reverse(make_linked_list([69, 88, 73]))
    '73 <- 88 <- 69'
    """
    return " <- ".join(str(line) for line in reversed(tuple(linked_list)))


if __name__ == "__main__":
    from doctest import testmod

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check truthiness of the sequence before calling: 'if elements_list:'.
  2. If an empty LinkedList is acceptable, construct LinkedList() directly and skip the builder.
  3. Catch generic Exception and match the message if the empty case must be normalized.

Example fix

# before
ll = make_linked_list(tokens)  # Exception when tokens == []
# after
ll = make_linked_list(tokens) if tokens else LinkedList()
Defensive patterns

Strategy: validation

Validate before calling

ll = make_linked_list(elements) if elements else LinkedList()

Type guard

def is_buildable(seq) -> bool:
    return bool(seq)

Try / catch

try:
    ll = make_linked_list(seq)
except Exception as e:
    if 'The Elements List is empty' not in str(e):
        raise
    ll = LinkedList()

Prevention

When it happens

Trigger: make_linked_list([]) or make_linked_list(()) — exactly the doctest case; passing None; passing a sequence after a filter/comprehension that removed everything.

Common situations: Splitting a string into tokens and getting zero tokens; processing optional CLI arguments where the user supplied none; mixing this builder with from_sequence's and assuming both raise ValueError (they do not).

Related errors


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