IndexError: pop index out of range in Python

avatar

Borislav Hadzhiev

Last updated: Apr 22, 2022

banner

Photo from Unsplash

IndexError: pop index out of range in Python #

The Python "IndexError: pop index out of range" occurs when we pass an index that doesn't exist in the list to the pop() method. To solve the error, pass an index that exists to the method or call the pop() method without arguments to remove the last item from the list.

indexerror pop index out of range

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ IndexError: pop index out of range result = my_list.pop(3)

The list has a length of 3. Since indexes in Python are zero-based, the first item in the list has an index of 0, and the last an index of 2.

abc
012
If we pass any positive index outside the range 0-2 to the pop() method, we would get an IndexError.

If you need to remove the last item in the list, call the method without passing it an index.

main.py
my_list = ['a', 'b', 'c'] result = my_list.pop() print(result) # 👉️ 'c' # 👇️ ['a', 'b'] print(my_list)

The list.pop method removes the item at the given position in the list and returns it.

If no index is specified, the pop() method removes and returns the last item in the list.

You can also use negative indices to count backwards, e.g. my_list.pop(-1) removes the last item of the list, and my_list.pop(-2) removes the second-to-last item.

Alternatively, you can check if an item at the specified index exists before passing it to pop().

main.py
my_list = ['a', 'b', 'c'] print(len(my_list)) # 👉️ 3 idx = 3 if len(my_list) > idx: result = my_list.pop(idx) print(result) else: # 👇️ this runs print(f'index {idx} is out of range')

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If a list has a length of 3, then its last index is 2 (because indexes are zero-based).

This means that you can check if the list's length is greater than the index you are passing to pop().

Note that calling pop() on an empty list also causes an error.

An alternative approach to handle the error is to use a try/except block.

main.py
my_list = ['a', 'b', 'c'] idx = 3 try: result = my_list.pop(idx) except IndexError: # 👇️ this runs print(f'index {idx} is out of range')

If calling the pop() method with the provided index raises an IndexError, the except block is run, where we can handle the error or use the pass keyword to ignore it.

Conclusion #

The Python "IndexError: pop index out of range" occurs when we pass an index that doesn't exist in the list to the pop() method. To solve the error, pass an index that exists to the method or call the pop() method without arguments to remove the last item from the list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.