If you’ve written more than 50 lines of Python, you’ve hit it: IndexError: list index out of range. It’s the second-most-Googled Python error after SyntaxError — and almost every BSIT …
Read more
Python IndexError — Fixes & Solutions
Python IndexError is one of the most common runtime errors Python developers encounter — typically thrown when you try to access a list, tuple, or string element using an index that's out of range. This hub collects fixes for the most common IndexError scenarios across base Python, pandas, NumPy, and common third-party libraries.
Common IndexError causes covered here
- List index out of range — accessing
list[i] when i >= len(list) - String index out of range — slicing or indexing beyond string length
- pandas IndexError —
.iloc[], .loc[], and DataFrame row access issues - NumPy IndexError — multi-dimensional array indexing errors
- Tuple index out of range — accessing tuple elements that don't exist
- pop from empty list — calling
list.pop() on an empty list
Quick prevention checklist
- Always validate index against
len() before accessing - Use
try/except IndexError for user-supplied indices - Prefer
.get() patterns or default fallbacks where possible - Iterate with
enumerate() instead of manual index tracking
New IndexError fixes are added regularly — bookmark this page or check the FAQ below for the most common questions.
Frequently Asked Questions
What does "list index out of range" mean in Python?
"IndexError: list index out of range" means you tried to access a position in a list that does not exist. Python lists are 0-indexed, so a list with 3 items has valid positions [0], [1], [2] — accessing [3] raises this error. Most often it is caused by: (1) an off-by-one bug in a loop, (2) accessing the list AFTER you popped items off, (3) assuming the list has more items than your data actually contains.
What is the difference between IndexError and KeyError?
IndexError is for SEQUENCES accessed by POSITION (list, tuple, string, bytes, numpy array). KeyError is for MAPPINGS accessed by NAME (dict, set, defaultdict, Counter, OrderedDict). my_list[5] on a 3-item list raises IndexError. my_dict['missing'] when 'missing' is not a key raises KeyError. Both inherit from LookupError so try/except LookupError catches both.
How do I avoid IndexError when accessing a list?
Three clean patterns: (1) Check length first: if len(my_list) > index: my_list[index]. (2) Use slicing instead — my_list[5:6] returns [item] or [] safely; never raises IndexError. (3) For "if the list has any items" — use if my_list: my_list[0]. Empty list is falsy in Python.
Why does my code work locally but raise IndexError in production?
99% of the time, the input data is different in production. Your local test data had 5 columns in every CSV row; production has rows with 4. Your test API response always had 3 items; the real API sometimes returns 0 or 1. Add explicit length checks before indexing: if len(row) < expected_count: skip_or_log(). Never assume data shape — always verify.
Does slicing raise IndexError?
No. Slicing in Python NEVER raises IndexError — it gracefully returns whatever is available, even an empty list. my_list[5:10] on a 3-item list returns []. my_list[100:200] also returns []. This is why slicing is the safer alternative for "give me the first N items, however many there are." Use my_list[:N] instead of accessing individual indices.
What is the difference between IndexError and StopIteration?
IndexError comes from indexed access on a sequence (my_list[5]). StopIteration comes from iterating with next() — next(iter_obj) raises StopIteration when the iterator is exhausted. In a regular for loop, StopIteration is caught silently; in manual next() calls, you must handle it. Generators raise StopIteration when their function returns. They look similar but have different mechanisms.
How do I handle empty results from split() / find_all() / split lines?
These functions return lists that can be shorter than you expect — or even empty. Before indexing, validate length: parts = text.split(','); if len(parts) >= 3: city = parts[2]; else: city = 'Unknown'. For BeautifulSoup: items = soup.find_all('div'); if items: first = items[0]. For regex groups: match = re.match(pattern, text); if match: group = match.group(1) (groups are not indexed by position in the standard re module — m.group(0), m.group(1) work like attributes, not indices, but failed matches raise AttributeError instead).
How often is this IndexError reference updated?
This reference is being actively built throughout 2026 — new IndexError fix posts are added weekly, starting with the most-searched patterns ("list index out of range" first). Existing posts are revised when major library versions ship changes. This page was last refreshed in May 2026.