6 Ways to Remove Python List Elements

Problem Formulation and Solution Overview

This article will show you how 6 ways to remove List elements in Python.

To make it more interesting, we have the following running scenario:

Suppose you have a Christmas List containing everyone to buy a gift for. Once a gift is purchased, remove this person from the List. Once all gifts have been purchased, remove the entire List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

πŸ’¬ Question: How would we write code to remove items from a Python List?

We can accomplish this task by one of the following options:


Method 1: Use the del Keyword

This method uses Python’s del Keyword and highlights its ability to remove one List element and all List elements.

Remove One List Element

In this scenario, Asa's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
del xmas_list[3]
print(xmas_list)

As shown on the highlighted line, Asa is removed from the List by using del, referencing xmas_list and specifying Asa’s location ([3]).

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Inger', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
del xmas_list
print(xmas_list)

As shown on the highlighted line, all elements of xmas_list are removed by using del and referencing xmas_list.

When xmas_list is output to the terminal, the following error is generated.

NameError: name 'xmas_list' is not defined

πŸ’‘Note: This error is generated because the variable xmas_list no longer exists in memory.


Method 2: Use remove() and a For Loop

This example uses the remove() function in conjunction with a for loop to remove one List element and all List elements.

Remove One List Element

In this scenario, Elin's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.remove('Elin')
print(xmas_list)

As shown on the highlighted line, Elin is removed from the List using the remove() function and passing Elin’s name as an argument.

When xmas_list is output to the terminal, the following displays.

['Anna', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
for item in xmas_list.copy():
    xmas_list.remove(item)
print(xmas_list)

As shown on the first highlighted line, a for loop is instantiated. This loop declares a shallow copy of the List to iterate.

On each iteration, the remove() function is called and passed the current name in xmas_list as an argument (see below) and removed.

For example:

Anna
Elin
Inger
Asa
Sofie
Gunnel
Linn

When xmas_list is output to the terminal, an empty List displays.

[]

πŸ’‘Note: A shallow copy creates a reference to the original List. For further details, view the video below.


Method 3: Use slicing

This method uses slicing to remove one List element and all List elements.

Remove One List Element

In this scenario, Anna's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = xmas_list[1:]
print(xmas_list)

As shown on the highlighted line, Anna is removed from the List using slicing.

When xmas_list is output to the terminal, the following displays.

['Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = []
print(xmas_list)

As shown on the highlighted line, all List elements are removed by declaring an empty List.

When xmas_list is output to the terminal, an empty List displays.

[]

Method 4: Use pop()

This method uses the pop() function to remove one List element and all List elements.

Remove One List Element

In this scenario, Linn's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.pop()
xmas_list.pop(2)
print(xmas_list)

As shown on the first highlighted line, the pop() method is appended to the xmas_list. This lets Python know to remove a List element from said List. Since no element is specified, the last element is removed (Linn).

On the second highlighted line, the pop() method is appended to the xmas_list and passed one (1) argument: the element to remove (2). This action removes Inger.

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Asa', 'Sofie', 'Gunnel']

πŸ’‘Note: Both Linn and Inger are no longer in xmas_list.

Remove All List Elements

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']

for i in xmas_list.copy():
    xmas_list.pop()
print(xmas_list)

As shown on the first highlighted line, a for loop is instantiated. This loop declares a shallow copy of the List to iterate.

On each iteration, the pop() method is called. Since no argument is passed, the last element is removed.

When xmas_list is output to the terminal, an empty List displays.

[]

Method 5: Use List Comprehension

This method uses List Comprehension to remove all List elements that do not meet the specified criteria.

Remove One List Element

In this scenario, Gunnel's gift has been purchased and will be removed from the List.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list = [value for value in xmas_list if value != 'Gunnel']
print(xmas_list)

When xmas_list is output to the terminal, the following displays.

['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Linn']

πŸ’‘Note: To remove all List elements, pass it empty brackets as shown follows: (xmas_list = []).


Method 6: Use clear()

This method uses clear() to remove all List elements.

In this scenario, all gifts have been purchased, and all List elements will be removed.

xmas_list = ['Anna', 'Elin', 'Inger', 'Asa', 'Sofie', 'Gunnel', 'Linn']
xmas_list.clear()
print(xmas_list)

As shown on the highlighted line, all List elements are removed by appending the clear() function to xmas_list.

When xmas_list is output to the terminal, an empty List displays.

[]

Summary

This article has provided six (6) ways to remove List elements to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programming Humor

πŸ’‘ Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.

~~~

  • Question: Why do Java programmers wear glasses?
  • Answer: Because they cannot C# …!

Feel free to check out our blog article with more coding jokes. πŸ˜‰