{"record":{"id":"9c13a275d63f8632","repo":"TheAlgorithms/Python","slug":"solve-simultaneous-requires-n-lists-of-length-n","errorCode":null,"errorMessage":"solve_simultaneous() requires n lists of length n+1","messagePattern":"solve_simultaneous\\(\\) requires n lists of length n\\+1","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"maths/simultaneous_linear_equation_solver.py","lineNumber":81,"sourceCode":"    >>> solve_simultaneous([])\r\n    Traceback (most recent call last):\r\n        ...\r\n    IndexError: solve_simultaneous() requires n lists of length n+1\r\n    >>> solve_simultaneous([[1, 2, 3],[1, 2]])\r\n    Traceback (most recent call last):\r\n        ...\r\n    IndexError: solve_simultaneous() requires n lists of length n+1\r\n    >>> solve_simultaneous([[1, 2, 3],[\"a\", 7, 8]])\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: solve_simultaneous() requires lists of integers\r\n    >>> solve_simultaneous([[0, 2, 3],[4, 0, 6]])\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: solve_simultaneous() requires at least 1 full equation\r\n    \"\"\"\r\n    if len(equations) == 0:\r\n        raise IndexError(\"solve_simultaneous() requires n lists of length n+1\")\r\n    _length = len(equations) + 1\r\n    if any(len(item) != _length for item in equations):\r\n        raise IndexError(\"solve_simultaneous() requires n lists of length n+1\")\r\n    for row in equations:\r\n        if any(not isinstance(column, (int, float)) for column in row):\r\n            raise ValueError(\"solve_simultaneous() requires lists of integers\")\r\n    if len(equations) == 1:\r\n        return [equations[0][-1] / equations[0][0]]\r\n    data_set = equations.copy()\r\n    if any(0 in row for row in data_set):\r\n        temp_data = data_set.copy()\r\n        full_row = []\r\n        for row_index, row in enumerate(temp_data):\r\n            if 0 not in row:\r\n                full_row = data_set.pop(row_index)\r\n                break\r\n        if not full_row:\r\n            raise ValueError(\"solve_simultaneous() requires at least 1 full equation\")\r","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/simultaneous_linear_equation_solver.py#L63-L99","documentation":"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.","triggerScenarios":"Calling solve_simultaneous([]). Typical when equations are built from parsed user text or generated rows and the loop produced nothing.","commonSituations":"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.","solutions":["Check for non-empty equations before calling: if equations: solve_simultaneous(equations)","Fix the parser/producer so it either produces rows or raises its own descriptive error","Catch IndexError specifically if you must handle it after the fact"],"exampleFix":"# before\nsolutions = solve_simultaneous(eq_rows)\n\n# after\nif not eq_rows:\n    raise InputError('no equations were parsed')\nsolutions = solve_simultaneous(eq_rows)","handlingStrategy":"validation","validationCode":"if not equations:\n    raise ValueError('no equations parsed')\nsolve_simultaneous(equations)","typeGuard":"def is_non_empty_equations(value: object) -> bool:\n    return isinstance(value, list) and len(value) > 0","tryCatchPattern":"try:\n    solve_simultaneous(equations)\nexcept IndexError as e:\n    # note: shape/emptiness errors are IndexError here\n    logger.error('bad equations input: %s', e)","preventionTips":["Remember this family raises IndexError, not ValueError","Fail loudly in your parser when zero equations are produced","Write tests for empty-input parsing"],"tags":["python","math","linear-algebra","index-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}