Removing Elements From a List
The clear
, pop
, and remove
list methods mutate (change) the
original list.
clear
Example
The general syntax for this method is:
list_name.clear()
clear
removes all elements from a list. The list still exists, but it is
empty.
No arguments go inside the ()
.
|
|
Output
[]
0
pop
Examples
The general syntax for this method is:
list_name.pop(index)
pop
removes the element at index
from the list. However, providing an
index
value is optional. If left blank, pop()
removes the last item
from the list.
In addition to removing an item from the list, pop
also returns that
value. This means list_name.pop(index)
can be printed or assigned to a
variable.
a_list = [100, 99, 98, 97, 96]
a_list.pop(2) # Removes and returns the element at index 2
print(a_list)
removed_element = a_list.pop() # Removes and returns the last element
print(removed_element)
print(a_list)
Output
[100, 99, 97, 96]
96
[100, 99, 97]
remove
Examples
The general syntax for remove
is:
list_name.remove(value)
This method removes the FIRST element in a list that matches value
.
value
may be of any data type.
If Python does not find value
inside the list, it throws an error, and the
program will likely crash.
|
|
Output
[89, 87, 88, 89, 80, 88]
Line 6:
b_list.remove(77) # Throws an error.
ValueError: list.remove(x): x not in list
To remove ALL elements that match value
, we must repeatedly call the
method. One approach is to use a while
loop.
|
|
Output
[89, 87, 88, 89, 80, 88]
[89, 87, 89, 80, 88]
[89, 87, 89, 80]