Ad Space — Top Banner

IndexError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

An IndexError means you tried to access an item in a list (or other sequence) using a position number that does not exist. For example, if a list has 3 items, the valid positions are 0, 1, and 2. If you ask for position 3 or higher, Python raises an IndexError because there is nothing there.

Affected Models

  • Python 2.x
  • Python 3.x
  • All Python versions

Common Causes

  • Using an index that is equal to or higher than the length of the list (lists start at index 0, not 1)
  • Trying to access an item from an empty list (there is nothing at any index if the list is empty)
  • Using a loop counter that goes one step too far — the classic off-by-one error
  • Assuming a list always has a certain number of items without checking first
  • Using a negative index that goes further back than the start of the list

How to Fix It

  1. Remember that Python lists start at index 0. A list with 5 items has valid indexes 0, 1, 2, 3, and 4. Index 5 does not exist.

    The last valid index is always len(my_list) - 1.

  2. Check if the list is empty before accessing items. Use 'if my_list:' or 'if len(my_list) > 0:' before trying to get an item.

    Accessing any index on an empty list will always cause an IndexError.

  3. If you are looping through a list with a range, use range(len(my_list)) instead of guessing the length. This automatically gives you the correct range.

    Better still, loop directly over the list items: 'for item in my_list:' — then you never deal with indexes at all.

  4. If you need to safely get an item without crashing, use a try/except block to catch the IndexError and handle it gracefully.

    Example: try: value = my_list[i] except IndexError: value = None

  5. Double-check any index variable. If it comes from user input or a calculation, make sure it is within the valid range (0 to len(my_list) - 1) before using it.

    You can clamp a value: safe_index = max(0, min(i, len(my_list) - 1))

When to Call a Professional

IndexErrors are always something you can fix yourself. The most common fix is to check the length of the list before accessing items. Use len(my_list) to find out how many items are in the list.

Frequently Asked Questions

Why does Python start counting list items from 0 instead of 1?

This is a design choice inherited from older programming languages like C. Starting at 0 makes certain mathematical operations simpler inside the computer. Almost every modern programming language uses 0-based indexing, so it is worth getting used to.

What does 'list index out of range' mean exactly?

It means you used an index number that falls outside the range of valid positions in the list. The valid range starts at 0 and ends at the total number of items minus one. Any number outside that range is 'out of range'.

Can I use negative indexes in Python?

Yes — Python allows negative indexes. Index -1 gives you the last item, -2 gives the second-to-last, and so on. But if you go too far negative (like -10 on a 3-item list), you still get an IndexError.