Remove all elements from a List before Index in Python

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Photo from Unsplash

Remove all elements from a List before Index in Python #

To remove all elements from a list before 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 index + 1 to the end.
  3. The new list will only contain the elements after the specified index.
main.py
my_list = ['Alice', 'Bob', 'Carl', 'Dean'] index = my_list.index('Bob') print(index) # 👉️ 1 after_index = my_list[index+1:] print(after_index) # 👉️ ['Carl', 'Dean']

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 after_index = my_list[index:] print(after_index) # 👉️ ['Carl', 'Dean']

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('Evelyn') + 1 except ValueError: # 👇️ this runs print('index not in List') index = None after_index = my_list[index:] print(after_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.

Since we want to remove all elements from the list before the specified index and the start value is inclusive, we had to add 1.

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

If 1 is not added to the index, the value at the specified index is also included.

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

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

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

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

We only specified a stop value, so the statement removes all elements from the list before the stop 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.