Create an account


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 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.

YouTube Video


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.

YouTube Video


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.


[]

YouTube Video


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.


[]

YouTube Video


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 = []).

YouTube Video


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.


[]

YouTube Video


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. ?



https://www.sickgaming.net/blog/2022/10/...-elements/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] 5 Expert-Approved Ways to Remove Unicode Characters from a Python Dict xSicKxBot 0 2 3 hours ago
Last Post: xSicKxBot
  [Tut] 4 Best Ways to Remove Unicode Characters from JSON xSicKxBot 0 16 12-03-2025, 03:06 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,049 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,967 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 2,102 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,950 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,553 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,693 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,603 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,508 04-22-2023, 06:10 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016