Introduction
This tutorial will go through some common ways for removing elements from Python arrays/lists.
Arrays or Lists?
Python's built-in sequence representation is a list, defined as a heterogenous sequence of elements, where each element has a definitive index in the sequence. To use arrays, you'd have to import the array
module, which does ship with Python itself, but lists are far more commonly used.
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. It's worth noting that many of these methods work both for an array and a list!
Using remove()
Appropriately, the remove()
function can be used 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]
Using pop()
The pop()
function 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.
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.
Using del
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, though, arrays don't support this. 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:
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]
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 with:
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:
Free eBook: Git Essentials
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!
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.