{"record":{"id":"96ed916e082b9ba4","repo":"TheAlgorithms/Python","slug":"list-index-out-of-range-96ed91","errorCode":null,"errorMessage":"list index out of range","messagePattern":"list index out of range","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/stacks/infix_to_prefix_conversion.py","lineNumber":93,"sourceCode":"    print_width = max(len(infix), 7)\n\n    # Print table header for output\n    print(\n        \"Symbol\".center(8),\n        \"Stack\".center(print_width),\n        \"Postfix\".center(print_width),\n        sep=\" | \",\n    )\n    print(\"-\" * (print_width * 3 + 7))\n\n    for x in infix:\n        if x.isalpha() or x.isdigit():\n            post_fix.append(x)  # if x is Alphabet / Digit, add it to Postfix\n        elif x == \"(\":\n            stack.append(x)  # if x is \"(\" push to Stack\n        elif x == \")\":  # if x is \")\" pop stack until \"(\" is encountered\n            if len(stack) == 0:  # close bracket without open bracket\n                raise IndexError(\"list index out of range\")\n\n            while stack[-1] != \"(\":\n                post_fix.append(stack.pop())  # Pop stack & add the content to Postfix\n            stack.pop()\n        elif len(stack) == 0:\n            stack.append(x)  # If stack is empty, push x to stack\n        else:  # while priority of x is not > priority of element in the stack\n            while stack and stack[-1] != \"(\" and priority[x] <= priority[stack[-1]]:\n                post_fix.append(stack.pop())  # pop stack & add to Postfix\n            stack.append(x)  # push x to stack\n\n        print(\n            x.center(8),\n            (\"\".join(stack)).ljust(print_width),\n            (\"\".join(post_fix)).ljust(print_width),\n            sep=\" | \",\n        )  # Output in tabular format\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/stacks/infix_to_prefix_conversion.py#L75-L111","documentation":"Explicitly raised in the loop of infix_to_postfix() inside data_structures/stacks/infix_to_prefix_conversion.py:93 when a ')' is scanned while the operator stack is empty — a close bracket with no possible matching open bracket. The message intentionally mimics CPython's builtin IndexError text, but this is a manual raise, not an accidental list access. Note the function also prints a tabular trace, so partial output precedes the raise.","triggerScenarios":"Calling the conversion with an expression like ')1+2' or '1+2)' where a ')' appears before any '(' has been pushed. Only a ')' with a completely empty stack triggers it; a ')' with operators on the stack pops normally.","commonSituations":"Feeding unvalidated user input or truncated strings to the prefix converter; unlike infix_to_postfix_conversion.py, this variant does NOT pre-check balanced parentheses, so unbalanced input reaches the loop.","solutions":["Pre-validate with balanced_parentheses() from infix_to_postfix_conversion before calling this function","Strip/repair stray ')' characters in the input pipeline","Catch IndexError (or ValueError for the sibling 'invalid expression' error) around the call and report the expression as malformed"],"exampleFix":"// before\nresult = infix_to_postfix(')1+2')  # IndexError\n\n# after\nfrom data_structures.stacks.infix_to_postfix_conversion import balanced_parentheses\nif not balanced_parentheses(expr):\n    raise ValueError(f'unbalanced expression: {expr!r}')\nresult = infix_to_postfix(expr)","handlingStrategy":"validation","validationCode":"from data_structures.stacks.infix_to_postfix_conversion import balanced_parentheses\nif not balanced_parentheses(expr):\n    raise ValueError(f'malformed expression: {expr!r}')\nresult = infix_to_postfix(expr)","typeGuard":null,"tryCatchPattern":"try:\n    result = infix_to_postfix(expr)\nexcept (IndexError, ValueError):\n    # this converter lacks a pre-check; both errors mean unbalanced parens\n    return None","preventionTips":["This prefix converter has NO built-in balance check — always pre-validate","Expect partial tabular print output before the raise if stdout is being captured"],"tags":["stack","expression-parsing","index-error","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}