Introduction
Python's built-in sequence representation is a list, defined as a heterogeneous sequence of elements, where each element has a definitive index in the sequence.
Note: Heterogeneous sequence means that one list can contain elements of different types.
To use arrays, you'd have to import the array
module, which does ship with Python itself, but lists are far more commonly used.
Advice: To learn more about lists in Python, read our "Guide to Lists in Python"
Additionally - since the list syntax looks a lot like the syntax you'd use to define arrays in other programming languages - the terms "array" and "list" are oftentimes used interchangeably, even though they're not the same data structure.
Note: It's worth noting that many of these methods work both for an array and a list!
In this article, we'll go through some common ways for removing elements from Python arrays/lists.
Approach #1 - Using remove() Method
We can use the remove()
method on any array or list in Python. To use it, we can simply pass the value of the element we want to remove. Let's imagine we have the following array:
array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
To remove, say, element 40
, we would simply write:
array.remove(40)
The result is the same array without the value 40
:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
Approach #2 - Using pop() Method
Another way we can remove elements from list/array in Python is by using the pop()
method. It accepts the index of the element we want to remove. If we had the same array/list as before (with values from 10 to 100), we could write something like the following:
index = 3
array.pop(index)
If we printed the result of the pop()
method, it would be the value 40
:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
Similarly to how pop()
works in the stack data structure, here pop()
also returns the value that it had just removed.
Advice: To learn more about stacks in Python, read our article "Stacks and Queues in Python"
The only difference is that with arrays, we can remove an arbitrary element. With stacks, only the top element (i.e. the last one added) can ever be removed.
Approach #3 - Using del Keyword
del
is a Python keyword used for deleting objects. Its exact behavior changes depending on the context, so we can also use it to remove list elements.
Important: Arrays don't support the del
keyword!
Once again, let's take the same array and index as before:
array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
index = 3
To remove the element at index 3
, we simply type the following:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
del array[index]
If we now printed the contents of our array, we would get the following output:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
Approach #4 - Using numpy Arrays
NumPy arrays are commonly used (especially in machine learning), so let's show one of the ways to remove an element from a numpy
array. Before using numpy
, it is necessary to import it:
import numpy as np
To create a numpy
array, we can wrap our current list using np.array()
as such:
a = np.array(array)
Alternatively, we could also declare a new array inside the method call itself:
a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
Now to remove an element at index 3
, we use the following code:
index = 3
a = np.delete(a, index)
delete()
is a static method declared in the numpy
module. It accepts the array and the index of the element to remove.
The method returns a new array without the removed element:
[10, 20, 30, 50, 60, 70, 80, 90, 100]
Conclusion
There are different ways to remove a list element in Python. Sometimes we might want to remove an element by index and sometimes by value. Sometimes we're using Python's default array and sometimes a numpy
array.
In all these cases, it's good to have multiple options to help us decide which of the techniques to use.