Python IndexError on sys.argv: 3 Fixes (2026)

Python IndexError on sys.argv: 3 Fixes (2026)

Python IndexError on sys.argv means you tried to read more command-line arguments than the user provided. The classic case: filename = sys.argv[1] when the user ran the script with no …

Read more

NumPy IndexError: Array Index Out of Bounds (2026)

NumPy IndexError Array Index Out of Bounds (2026)

IndexError on NumPy array access (e.g. arr[100] when arr has 50 elements, or arr[1, 5] when arr is shape (3, 3)) is one of the most common NumPy errors. The …

Read more

Python IndexError on .split() Result (2026)

Python IndexError on .split() Result (2026)

Python IndexError after calling .split() means the resulting list has fewer parts than your code expected. This is the classic “log parser breaks on the one line with a weird …

Read more

Python IndexError on String and Tuple Indexing (2026)

Python IndexError on String and Tuple Indexing (2026)

Strings and tuples in Python both raise IndexError on out-of-range indexing, but never on slicing. Knowing the difference between s[5] (raises) and s[5:10] (returns empty string) is the foundation of …

Read more

Python IndexError pop from empty list: 4 Fixes (2026)

Python IndexError pop from empty list 4 Fixes (2026)

IndexError: pop from empty list happens when you call list.pop() on an empty list. This is one of the most common Python bugs in queues, stacks, work-stealing loops, and item-by-item …

Read more

Selenium IndexError on find_elements[0]: Fix (2026)

Selenium IndexError on find_elements[0] Fix (2026)

You called driver.find_elements(By.CSS_SELECTOR, “.price”)[0].text and Python crashed with IndexError because the page hadn’t finished loading or the selector matched zero elements. Selenium’s find_elements (plural) returns an empty list when nothing …

Read more

Pandas iloc IndexError: Positional Out of Bounds (2026)

Pandas iloc IndexError Positional Out of Bounds (2026)

You called df.iloc[100] but the DataFrame has only 50 rows, so pandas threw IndexError: single positional indexer is out-of-bounds. Note: .iloc raises IndexError (position-based), while .loc raises KeyError (label-based). Knowing …

Read more

Frequently Asked Questions

What is Python IndexError "list index out of range"?
Python raises IndexError when you try to access an index in a list, tuple, or string that does not exist. If your list has 3 items, the valid indexes are 0, 1, and 2. Accessing my_list[3] raises IndexError: list index out of range. The same applies to negative indexes: my_list[-4] on a 3-item list also raises IndexError. The fix is always either (1) check the length before accessing with if i < len(my_list) or (2) iterate with for item in my_list instead of indexing.
How is IndexError different from KeyError?
Both inherit from LookupError but apply to different containers. IndexError is for sequence types accessed by integer position (lists, tuples, strings, pandas .iloc[]). KeyError is for mapping types accessed by label (dicts, pandas DataFrames by column name, pandas .loc[]). If you wrote my_data[2] and got IndexError, you are indexing a sequence. If you wrote my_data["name"] and got KeyError, you are looking up a mapping.
What is an off-by-one error and how does it cause IndexError?
An off-by-one error happens when your loop or index calculation is exactly 1 too high or 1 too low for the actual size of the data. The classic example is for i in range(1, len(my_list) + 1) when you meant for i in range(len(my_list)). The first loop tries to access my_list[len(my_list)] on the last iteration, which is one past the last valid index and raises IndexError. The defensive fix is to always use for item in my_list when you do not need the index, or for i, item in enumerate(my_list) when you do.
How do I safely access list elements to avoid IndexError?
Three idiomatic patterns: (1) if i < len(my_list): value = my_list[i] for explicit bounds checks. (2) value = my_list[i] if i < len(my_list) else default for one-liner default fallback. (3) try: value = my_list[i]\nexcept IndexError: value = default when you need to log the failure or handle it differently. Avoid my_list[i] if i in my_list because in checks for value membership, not index validity.
Why does pandas .iloc[] raise IndexError?
pandas .iloc[] is positional indexing, so it raises IndexError when the position is out of range, just like a regular list. df.iloc[1000] on a 500-row DataFrame raises IndexError. .loc[] on the other hand is label-based and raises KeyError when the label is missing. Watch out: after filtering or dropping rows, the integer positions shift while the labels stay the same. df_filtered.iloc[5] may now point to a different row than df.iloc[5].
What is the difference between IndexError on a list vs a string?
The behavior is identical: both raise IndexError when you access an out-of-range numeric position. The only practical difference is the error message: lists say "list index out of range", strings say "string index out of range". For strings, this often happens when you assume a string has at least N characters: name[0] on an empty string from user input raises IndexError. Defensive fix: name[0] if name else '' or check if len(name) > 0 first.
Can I catch IndexError or should I prevent it?
Prefer preventing it with a length check (if i < len(my_list)) for normal control flow. The check is cheaper than raising and catching an exception, and the intent reads more clearly. Catch IndexError with try/except when the out-of-range access represents a genuine error you want to log or handle distinctly from the happy path (parsing untrusted input, deserializing data with unknown shape, defensive coding around third-party return values). Use both together when defensive: validate length, log the unexpected case, then continue.