Lists in Python are used to store a collection of data. They are encapsulated in square brackets [] and each element is separated by a comma. There may be situations where you need to remove a specific element in a list. Python has 3 methods for removing elements in a list; the remove() method, pop() method, and clear() method. In the tutorial, we will discuss how to use all 3 methods to remove elements in a list. Also, you will learn how to use the del keyword to remove elements in a list.Â
By the end of this tutorial, here’s what you will learn
- Understanding list indexing
- Using the remove method
- Using the pop method
- Using the clear method
- Using the del keyword
Let’s hop in.
Understanding List Indexing
We will begin with understanding how a list is defined. As mentioned earlier, a list is encapsulated in square brackets, and the elements are separated by commas. An example of a list is shown below.
List_1 = [1, 2, ‘H2k’, 4.3, True, 8]
The individual elements in the list can be accessed using indexing. It is important to note that Python indexes from 0. In the list defined above
- The first element, 1 has an index of 0.
- The second element, 2 has an index of 1.
- H2k has an index of 2,
- 4.3 has an index of 3
- True is of index 4
- 8 has an index of 5.
When indexing from behind Python indexes from -1, then -2, -3, and so on. In the above example,
- 8, the last element can also be accessed with index -1
- True has an index of -2
- 4.3 has an index of -3
- H2k has an index of -4
- 2 has an index of -5
- 1 has an index of -6
Now you know how indexes work, let’s begin with seeing how to remove an element in a list using the remove() method.
Using the remove() method in Python
The remove method is used to remove the first instance of the element passed as an argument. It is important to note that the remove method does not return a value. It is however seen as an operation performed on the list under the hood. Let’s see an example
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the first instance of 2 in the list list_1.remove(2) Â #print the list print(list_1)
Output: [1, 'H2k', 4.3, 2, True, 2, 8]
Notice that value 2 was passed as an argument to the remove() method. It however removes only the first instance of 2 in the list while the other 2’s remain present. This is how the remove method works. It removes only the first instance of the value passed as an argument.
If an element that is not in the list is passed as an argument of the remove() method, the program returns a program error. Let’s try to remove 400, which is not an element in the list.
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the first instance of 2 in the list list_1.remove(400) Â #print the list print(list_1)
Output:
Traceback (most recent call last):
File "c:/Users/DELL/Documents/VS codes files/fresh.py", line 5, in <module>
list_1.remove(400)
ValueError: list.remove(x): x not in list
Removing an element in the list using the pop() method.
When you use the pop() method, it allows you to specify the index of the element you want to remove. By default, the pop() method removes the last index. In other words, when you do not pass any argument, a default argument of -1 is passed, thus removing the last element.
Let’s see an example where an argument is not passed.Â
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the last element list_1.pop() print(list_1)
Output: [1, 2, 'H2k', 4.3, 2, True, 2]
Notice that the last element, 8 has been removed.
Now let’s specify an index. Let’s say we wish to remove the boolean True. True is at index 5, therefore, 5 is passed as an argument to the pop() method. See the code to implement this below.Â
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the element on the 5th index list_1.pop(5) print(list_1)
Output:
[1, 2, 'H2k', 4.3, 2, 2, 8]
Notice that the boolean True has been removed.
If you pass an index that does not exist in the list, the pop method returns an IndexError. Let’s say we pass an index 10 when the pop() method is called on the list.Â
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the element on the 10th index list_1.pop(10) print(list_1)
Output:
Traceback (most recent call last):
File "c:/Users/DELL/Documents/VS codes files/fresh.py", line 5, in <module>
list_1.pop(10)
IndexError: pop index out of range
Using the clear() method.
The clear method is slightly different from the other two as this empties the list. See an example below.Â
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove all the element in the list list_1.clear() print(list_1)
Output:
[]
Using the del statement
The del statement is also used to remove elements in a list. The del keyword is however unique as it adds more flexibility to how the elements are removed.
You can decide to remove only one element in the list by indexing the particular element, as in del list[index]
You could also delete a range of elements in the list. You do that by declaring the list slice you wish to have removed after the del keyword. It follows this convention: del list[start: stop]
Let’s see a coding example of both situations.
First, let’s assume you want to remove the second element in the list using the del keyword, you can write the following code.
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the second element del list_1[1] print(list_1)
Output:
[1, 'H2k', 4.3, 2, True, 2, 8]
As seen, the second element (1) has been removed.
Let’s say this time, you wish to remove the 2nd to the 5th element. You declare the list slice after the del statement. The code is shown below
#define some list list_1 = [1, 2,'H2k', 4.3, 2, True, 2, 8] Â #remove the second to fifth element del list_1[1:5] print(list_1)
Output:
[1, True, 2, 8]
To wrap up
In this tutorial, you have learned how to remove elements in a list using the remove() method, pop() method, and clear() method. You also learned how to remove an element or a range of elements in a list using the del keyword. Remember that
- When using the remove() method, the first instance of the element that matches the argument pass is removed.
- When using the pop() method, you pass the index of the element you want to be removed. By default, it removes the last element.
- The clear() method removes all the elements in the list
- The del statement allows you to specify the elements to be removed through list indexing or list slicing.
If you’ve got any questions, feel free to leave them in the comment section and I’d do my best to answer them.