Remove all elements from a List after Index in Python

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Check out my new book

Remove all elements from a List after Index in Python #

To remove all elements from a list after specific index:

  1. Use the list.index() method to get the index of the value.
  2. Use list slicing to get a slice of the list from index 0 to index + 1.
  3. The new list will only contain the elements before the specified index.
main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] index = my_list.index('Bob') print(index) # 👉️ 1 before_index = my_list[:index + 1] print(before_index) # 👉️ ['Alice', 'Bob']

We used the list.index() method to get the index of a specific value in the list.

The list.index() method returns the index of the first item whose value is equal to the provided argument.
main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] index = my_list.index('Bob') print(index) # 👉️ 1

The method raises a ValueError if there is no such item in the list.

You can use a try/except statement to handle the scenario where the item is not in the list.

main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] try: index = my_list.index('Bob') + 1 except ValueError: print('index not in List') index = None print(index) # 👉️ 2 before_index = my_list[:index] print(before_index) # 👉️ ['Alice', 'Bob']

And here is an example of how this works when we call list.index() with a value that isn't present in the list.

main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] try: index = my_list.index('Another') + 1 except ValueError: print('index not in List') index = None print(index) # 👉️ None before_index = my_list[:index] print(before_index) # 👉️ ['Alice', 'Bob', 'Carl', 'Dean']

Notice that we add 1 to the result of calling the list.index() method.

The syntax for list slicing is my_list[start:stop:step] where the start value is inclusive, and the stop value is exclusive.

main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] index = my_list.index('Bob') + 1 print(index) # 👉️ 2 before_index = my_list[:index] print(before_index) # 👉️ ['Alice', 'Bob']
Since the value for the stop index is exclusive, we add 1 to the index to exclude all elements after the specified index.

When a value for the start index is not specified, the list slice starts at index 0.

Note that this approach does not remove the elements from the original list, it returns a new list that only contains the elements before the specified index.

If you need to remove the elements from the list after the specified index, in place, use the del statement, e.g. del my_list[index:].

main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] index = my_list.index('Bob') + 1 del my_list[index:] print(my_list) # 👉️ ['Alice', 'Bob']

The del statement can be used with a list slice to remove multiple items from a list.

We only specified a start value, so the statement removes all elements from the list after the start index.

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.