TheAlgorithms/Python · error · IndexError

solve_simultaneous() requires n lists of length n+1

Error message

solve_simultaneous() requires n lists of length n+1

What it means

Raised by solve_simultaneous() in maths/simultaneous_linear_equation_solver.py when the equations argument is an empty list (len(equations) == 0). With no equations there is nothing to solve, and the n-lists-of-length-n+1 contract cannot be satisfied, so an IndexError is raised. Note the unusual choice of IndexError (not ValueError) for malformed input — catch accordingly.

Source

Thrown at maths/simultaneous_linear_equation_solver.py:81

    >>> solve_simultaneous([])
    Traceback (most recent call last):
        ...
    IndexError: solve_simultaneous() requires n lists of length n+1
    >>> solve_simultaneous([[1, 2, 3],[1, 2]])
    Traceback (most recent call last):
        ...
    IndexError: solve_simultaneous() requires n lists of length n+1
    >>> solve_simultaneous([[1, 2, 3],["a", 7, 8]])
    Traceback (most recent call last):
        ...
    ValueError: solve_simultaneous() requires lists of integers
    >>> solve_simultaneous([[0, 2, 3],[4, 0, 6]])
    Traceback (most recent call last):
        ...
    ValueError: solve_simultaneous() requires at least 1 full equation
    """
    if len(equations) == 0:
        raise IndexError("solve_simultaneous() requires n lists of length n+1")
    _length = len(equations) + 1
    if any(len(item) != _length for item in equations):
        raise IndexError("solve_simultaneous() requires n lists of length n+1")
    for row in equations:
        if any(not isinstance(column, (int, float)) for column in row):
            raise ValueError("solve_simultaneous() requires lists of integers")
    if len(equations) == 1:
        return [equations[0][-1] / equations[0][0]]
    data_set = equations.copy()
    if any(0 in row for row in data_set):
        temp_data = data_set.copy()
        full_row = []
        for row_index, row in enumerate(temp_data):
            if 0 not in row:
                full_row = data_set.pop(row_index)
                break
        if not full_row:
            raise ValueError("solve_simultaneous() requires at least 1 full equation")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check for non-empty equations before calling: if equations: solve_simultaneous(equations)
  2. Fix the parser/producer so it either produces rows or raises its own descriptive error
  3. Catch IndexError specifically if you must handle it after the fact

Example fix

# before
solutions = solve_simultaneous(eq_rows)

# after
if not eq_rows:
    raise InputError('no equations were parsed')
solutions = solve_simultaneous(eq_rows)
Defensive patterns

Strategy: validation

Validate before calling

if not equations:
    raise ValueError('no equations parsed')
solve_simultaneous(equations)

Type guard

def is_non_empty_equations(value: object) -> bool:
    return isinstance(value, list) and len(value) > 0

Try / catch

try:
    solve_simultaneous(equations)
except IndexError as e:
    # note: shape/emptiness errors are IndexError here
    logger.error('bad equations input: %s', e)

Prevention

When it happens

Trigger: Calling solve_simultaneous([]). Typical when equations are built from parsed user text or generated rows and the loop produced nothing.

Common situations: Parsing '2x+3y=8' style input where parsing yielded zero valid equations; empty database result mapped to coefficient rows; conditional row-generation where every condition failed.

Related errors


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