Remove element from list without Mutation in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove element from list without Mutation in Python #

Use a list comprehension to remove an element from a list without mutation, e.g. new_list = [item for item in my_list if item != 'value']. The list comprehension will return a new list that doesn't contain the specified item.

main.py
my_list = ['one', 'two', 'three', 'four'] # ✅ Remove element from list without mutation (list comprehension) new_list = [item for item in my_list if item != 'three'] print(new_list) # 👉️ ['one', 'two', 'four'] # ---------------------------------------- # ✅ Remove element from list without mutation (by creating a copy) list_copy = my_list.copy() list_copy.remove('three') print(list_copy) # 👉️ ['one', 'two', 'four'] # ✅ Remove element from list without mutation (using list slicing) # ---------------------------------------- index = my_list.index('three') print(index) # 👉️ 2 new_list = my_list[:index] + my_list[index + 1:] print(new_list) # 👉 ['one', 'two', 'four']

The first example uses a list comprehension to remove an element from a list without mutation.

main.py
my_list = ['one', 'two', 'three', 'four'] new_list = [item for item in my_list if item != 'three'] print(new_list) # 👉️ ['one', 'two', 'four']
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The list comprehension doesn't change the original list, it returns a new list that doesn't contain the specified value.

main.py
my_list = ['one', 'two', 'three', 'four'] new_list = [item for item in my_list if item != 'three'] print(new_list) # 👉️ ['one', 'two', 'four'] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

This approach is quite handy when you need to remove multiple items from the list without mutating it.

It's not as efficient if you only need to remove a single value. If the value is towards the beginning of the list, the list comprehension would needlessly iterate the entire list.

An alternative approach is to use the list.copy() method.

To remove an element from a list without mutating it:

  1. Use the list.copy() method to create a copy of the list.
  2. Use the list.remove() method to remove the element from the copy.
  3. The original list will remain unchanged.
main.py
my_list = ['one', 'two', 'three', 'four'] list_copy = my_list.copy() list_copy.remove('three') print(list_copy) # 👉️ ['one', 'two', 'four'] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

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.

The next step is to call the list.remove() method on the copy.

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

The remove() method mutates the list the method was called on and returns None.

We called the remove() method on the copy, so the original list remains unchanged.

This approach is more efficient than using a list comprehension if you only need to remove a single item from a list without mutating it.

The list.remove() method raises a ValueError if there is no such item.

You can use a try/except statement if you need to handle the scenario where the provided value is not in the list.

main.py
my_list = ['one', 'two', 'three', 'four'] list_copy = my_list.copy() try: list_copy.remove('fifty') except ValueError: pass print(list_copy) # 👉️ ['one', 'two', 'three', 'four'] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

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

Alternatively, you can use list slicing.

To remove an element from a list without mutation:

  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 = ['one', 'two', 'three', 'four'] index = my_list.index('three') print(index) # 👉️ 2 new_list = my_list[:index] + my_list[index + 1:] print(new_list) # 👉 ['one', 'two', 'four'] print(my_list) # 👉️ ['one', 'two', 'three', 'four']

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.