Remove an item from a list and return a new list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove an item from a list and return a new list in Python #

To remove an item from a list and return a new list:

  1. Use the list.copy() method to create a copy of the list.
  2. Use the list.remove() method to remove an item from the copy.
  3. The original list will remain unchanged.
main.py
my_list = ['first', 'second', 'third', 'fourth'] # ✅ remove item from list and get a new list (list.copy()) list_copy = my_list.copy() list_copy.remove('second') print(list_copy) # 👉️ ['first', 'third', 'fourth'] print(my_list) # 👉️ ['first', 'second', 'third', 'fourth'] # ---------------------------------- # ✅ remove item from list and return a new list (list slicing) index = my_list.index('second') new_list = my_list[:index] + my_list[index+1:] print(new_list) # 👉️ ['first', 'third', 'fourth'] print(my_list) # 👉️ ['first', 'second', 'third', 'fourth']

We used the list.copy() method to create a copy of the list.

The list.copy method returns a shallow copy of the object on which the method was called.

We can call the list.remove() method on the copy, so we don't change the original list.

The remove() method will change the copy and will return None.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

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

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

main.py
my_list = ['first', 'second', 'third', 'fourth'] list_copy = my_list.copy() try: list_copy.remove('fifth') except ValueError: pass print(list_copy) # 👉️ ['first', 'second', 'third', 'fourth'] print(my_list) # 👉️ ['first', 'second', 'third', 'fourth']

The provided value is not in the list, so the except block runs.

Alternatively, you can use list slicing.

To remove an item from a list and return a new list:

  1. Use list slicing to get a slice of the list up to the index of the element to be removed.
  2. Use list slicing to get a slice of the list after the element.
  3. Use the addition (+) operator to combine the two list slices.
main.py
my_list = ['first', 'second', 'third', 'fourth'] index = my_list.index('second') new_list = my_list[:index] + my_list[index+1:] print(new_list) # 👉️ ['first', 'third', 'fourth'] print(my_list) # 👉️ ['first', 'second', 'third', 'fourth']

We used list slicing to remove an element from a list without changing the list.

The syntax for list slicing is my_list[start:stop:step] where the start index is inclusive and the stop index is exclusive (up to, but not including).

The slice my_list[:index] starts at the beginning of the list and goes up to, but not including the index of the value to be removed.

The slice my_list[index + 1:] starts at the index after the index of the value to be removed and goes to the end of the list.

Notice that we added 1 to the index of the value, because the start index is inclusive and we want to exclude the value from the new list.

The last step is to use the addition (+) operator to combine the two list slices.

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.