TheAlgorithms/Python · error · ValueError

Input list must contain at least two elements

Error message

Input list must contain at least two elements

What it means

Raised by recursive_imply_list() when the input list has fewer than two elements. The function folds a chain of logical implication (a -> b -> c) left-associatively by calling imply_gate on the first two elements; a single element or empty list leaves nothing to imply, so it refuses rather than guessing an identity value.

Source

Thrown at boolean_algebra/imply_gate.py:80

    >>> recursive_imply_list([0, 0, 0])
    0
    >>> recursive_imply_list([0, 0, 1])
    1
    >>> recursive_imply_list([0, 1, 0])
    0
    >>> recursive_imply_list([0, 1, 1])
    1
    >>> recursive_imply_list([1, 0, 0])
    1
    >>> recursive_imply_list([1, 0, 1])
    1
    >>> recursive_imply_list([1, 1, 0])
    0
    >>> recursive_imply_list([1, 1, 1])
    1
    """
    if len(input_list) < 2:
        raise ValueError("Input list must contain at least two elements")
    first_implication = imply_gate(input_list[0], input_list[1])
    if len(input_list) == 2:
        return first_implication
    new_list = [first_implication, *input_list[2:]]
    return recursive_imply_list(new_list)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check len(input_list) >= 2 before calling.
  2. If a single element is meaningful for your logic, handle it yourself: return the element (or imply-identity convention) instead of delegating.
  3. Guard list-producing code paths (filters, slices) so they cannot emit <2 elements.

Example fix

# before
recursive_imply_list([1])  # ValueError: Input list must contain at least two elements

# after
result = flags[0] if len(flags) == 1 else recursive_imply_list(flags)
Defensive patterns

Strategy: validation

Validate before calling

if len(input_list) < 2:
    raise ValueError('need >= 2 boolean flags to imply')

Prevention

When it happens

Trigger: Calling recursive_imply_list([1]), recursive_imply_list([0]), or recursive_imply_list([]).

Common situations: Programmatically slicing boolean flag lists (e.g. conditions[1:]) and passing a too-short remainder, or degenerate user input that selects only one condition.

Related errors


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