Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 6 Ways to Remove Python List Elements
1
6 Ways to Remove Python List Elements

5/5 – (2 votes)

Problem Formulation and Solution Overview

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

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

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


[Image: xmas_lst_02.png]

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

[Image: 1f4ac.png] 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
Code:
del
Keyword and highlights its ability to remove one List element and all List elements.

Remove One List Element

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

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

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

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

Code:
['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.

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

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

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

Code:
NameError: name 'xmas_list' is not defined

[Image: 1f4a1.png]Note: This error is generated because the variable
Code:
xmas_list
no longer exists in memory.

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 2: Use remove() and a For Loop

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

Remove One List Element

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

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

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

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

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

Remove All List Elements

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

Code:
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
Code:
for
loop is instantiated. This loop declares a shallow copy of the List to iterate.

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

For example:

Code:
AnnaElinIngerAsaSofieGunnelLinn

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

Code:
[]

[Image: 1f4a1.png]Note: A shallow copy creates a reference to the original List. For further details, view the video below.

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 3: Use slicing

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

Remove One List Element

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

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

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

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

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

Remove All List Elements

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

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

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

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

Code:
[]

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 4: Use pop()

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

Remove One List Element

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

Code:
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
Code:
pop()
method is appended to the
Code:
xmas_list
. This lets Python know to remove a List element from said List. Since no element is specified, the last element is removed (
Code:
Linn
).

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

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

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

[Image: 1f4a1.png]Note: Both Linn and Inger are no longer in
Code:
xmas_list
.

Remove All List Elements

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

Code:
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
Code:
for
loop is instantiated. This loop declares a shallow copy of the List to iterate.

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

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

Code:
[]

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

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,
Code:
Gunnel's
gift has been purchased and will be removed from the List.

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

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

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

[Image: 1f4a1.png]Note: To remove all List elements, pass it empty brackets as shown follows: (
Code:
xmas_list = []
).

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 6: Use clear()

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

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

Code:
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
Code:
clear()
function to
Code:
xmas_list
.

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

Code:
[]

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

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

[Image: 1f4a1.png] 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. [Image: 1f609.png]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: