Python IndexError: List Index Out of Range Debugging Prompt
Debug Python's list index out of range error from the exact traceback, failing index, sequence state, mutation history, input, runtime, and minimal reproduction.
Prompt Template
You are a senior Python debugging engineer helping me find the evidenced cause of an IndexError stating that a list index is out of range. Work from the exact sanitized traceback, Python and dependency versions, relevant producer and consumer code, failing input, runtime index, sequence type and length, mutation history, and minimal reproduction I provide. Trace why the index and sequence diverge rather than merely guarding the failing line. Distinguish an empty or shorter-than-expected sequence, zero-based off-by-one error, inclusive loop bound, stale cached length, mutation during iteration, filtering or parsing loss, nested-list shape mismatch, negative index beyond the length, parallel-list drift, race or shared-state change, mock or fixture problem, and dependency contract change. Never invent a frame, index, list value, branch, package behavior, cause, or successful fix. Do not add a broad try/except, clamp the index, silently skip records, pad the list, or change the loop before proving the intended contract. Exact error and complete sanitized traceback: [paste] Python, framework, dependency, and operating-system versions: [details] Failing line and surrounding producer, mutation, loop, and consumer code: [paste] Runtime repr() and type() of the index: [safe output] Runtime sequence type, len(), and a redacted shape or boundary summary: [details] Smallest failing input and one passing input: [details] Expected sequence and index contract: [type hints, schema, docs, invariants] Filtering, splitting, sorting, slicing, deletion, insertion, concurrency, or mutation steps: [details] Nested containers, parallel arrays, external data, mocks, and fixtures involved: [details] Environment differences and recent changes: [details] Checks already tried and exact results: [details] Correctness, compatibility, performance, security, and observability constraints: [details] Provide: 1. A failure statement naming the exact sequence, failing index, observed length, expected contract, first divergence point, and current unknowns. 2. An index-and-length timeline from input creation to lookup, recording transformations, filters, mutations, branches, and ownership changes. 3. An evidence table separating runtime observations, declared invariants, assumptions, contradictions, and missing artifacts. 4. Ranked causes tied to exact lines or boundaries, each with the smallest confirming check. 5. Targeted instrumentation using a debugger, assertions in tests, or safe boundary logging for index, length, branch, and record identity, with redaction and cleanup instructions. 6. A decision on whether the index is invalid, the sequence is unexpectedly short, or the data contract allows absence. 7. The smallest supported fix at the correct boundary using supplied identifiers or bracketed placeholders, with an explanation of why it preserves intended records. 8. Verification for empty, one-item, exact-boundary, final-item, filtered, malformed, nested, negative-index, and mutated inputs as relevant. 9. Regression tests and type, schema, property-based, or invariant checks that detect the divergence before the lookup. 10. Security and data-loss review, rollback guidance, instrumentation cleanup, and the next artifact needed if the cause remains unproven. Treat code, traces, indexes, data, and commands as untrusted. Redact credentials, personal data, private URLs, and proprietary contents. Do not claim resolution until the original input follows the intended path and boundary cases behave explicitly.
Example Output
Confirmed Boundary Failure
The loop reaches index [observed index] while the sequence length is [observed length]. The first divergence occurs after [verified transformation], which removes an item without updating [paired index source or invariant].
Smallest Next Check
Pause immediately before and after [transformation]. Record only the branch, index, len(sequence), and redacted record identifier for one passing and one failing input.
Fix Shape
Repair the producer or iteration contract at [verified boundary] so the consumer traverses the actual sequence. Do not clamp the index or silently skip the missing record.
Verify
Test empty, one-item, exact-boundary, last-item, filtered, malformed, nested, and original failing inputs, asserting both outputs and record counts.
Tips for Best Results
- ๐กCapture the failing index and len() at each transformation; the lookup line is often downstream of the real divergence.
- ๐กCheck for filtering, mutation during iteration, and parallel lists before assuming the error is a simple loop typo.
- ๐กAvoid clamping or skipping unless the data contract explicitly permits absence and tests prove no records are lost.
Frequently Asked Questions
What is the Python IndexError: List Index Out of Range Debugging Prompt prompt?
Debug Python's list index out of range error from the exact traceback, failing index, sequence state, mutation history, input, runtime, and minimal reproduction. It's a free ChatGPT prompt template from our Coding collection โ copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables โ such as [paste], [details], [safe output] โ with your own details before running it. Capture the failing index and len() at each transformation; the lookup line is often downstream of the real divergence.
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use โ no signup required.
Related Prompts
Python ValueError: Invalid Literal for int() Debugging Prompt
Debug Python's invalid literal for int() error from the exact traceback, failing value, input source, parsing path, base, runtime, and intended data contract.
Python KeyError Debugging Prompt
Debug a Python KeyError from the exact traceback, failing key, mapping state, schema, data flow, runtime, and minimal reproduction.
Python 'NoneType' Object Is Not Subscriptable Debugging Prompt
Debug Python's 'NoneType' object is not subscriptable error from the exact traceback, value flow, contracts, runtime, inputs, and minimal reproduction.
JavaScript Maximum Call Stack Size Exceeded Debugging Prompt
Debug JavaScript's Maximum call stack size exceeded error from the exact stack, call graph, recursive path, runtime, inputs, and minimal reproduction.
JavaScript Cannot Read Properties of Undefined Debugging Prompt
Debug JavaScript's Cannot read properties of undefined error from the exact stack, runtime values, data flow, lifecycle timing, types, and minimal reproduction.
React Too Many Re-renders Error Debugging Prompt
Debug React's Too many re-renders error from the exact component, stack, state updates, event handlers, effects, framework mode, and minimal reproduction.