Remove the first and last items from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove the first and last items from a List in Python #

Use list slicing to remove the first and last items from a list, e.g. result = my_list[1:-1]. The new list will contain a slice of the original list without the first and last elements.

main.py
my_list = ['one', 'two', 'three', 'four'] # ✅ Remove first and last elements from a list result = my_list[1:-1] print(result) # 👉️ ['two', 'three] # ✅ Remove the first element from a list result_2 = my_list[1:] print(result_2) # 👉️ ['two', 'three', 'four'] # ✅ Remove the last element from a list result_3 = my_list[:-1] print(result_3) # 👉️ ['one', 'two', 'three']

We used list slicing to remove the first and last elements from a list.

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The slice my_list[1:-1] starts at the item at index 1 and goes up to, but not including the last item in the list.

main.py
my_list = ['one', 'two', 'three', 'four'] # ✅ Remove the first and last elements from a list result = my_list[1:-1] print(result) # 👉️ ['two', 'three]

If you only need to remove the first item from the list, start at index 1 and go til the end of the list.

main.py
my_list = ['one', 'two', 'three', 'four'] # ✅ Remove the first element from a list result_2 = my_list[1:] print(result_2) # 👉️ ['two', 'three', 'four']

When the stop index is not specified, the slice goes to the end of the list.

If you only need to remove the last item from the list, omit the start index and specify a stop index of -1.

main.py
my_list = ['one', 'two', 'three', 'four'] # ✅ Remove the last element from a list result_3 = my_list[:-1] print(result_3) # 👉️ ['one', 'two', 'three']

When the start index is not specified, the slice starts at index 0.

The slice in the example goes up to, but not including the last element in the list.

Alternatively, you can use the list.pop() method.

To remove the first and last elements from a list:

  1. Use the list.pop() method to remove the list item at index 0.
  2. Use the list.pop() method to remove the last list item.
  3. The pop() method will remove the first and last elements from the list.
main.py
my_list = ['one', 'two', 'three', 'four'] my_list.pop(0) my_list.pop() print(my_list) # 👉️ ['two', 'three']
As opposed to using list slicing, the list.pop() method mutates the original 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.

The list.pop() method raises an IndexError if the specified index is out of range.

If you need to handle the scenario where the index is not present in the list, use a try/except statement.

main.py
my_list = ['one', 'two', 'three', 'four'] try: my_list.pop(0) except IndexError: print('The list is empty') try: my_list.pop() except IndexError: print('The list is empty') print(my_list) # 👉️ ['two', 'three']

If the list is empty and we try to remove an item, the except block runs.

You can use the pass statement if you want to ignore the IndexError.

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.