Remove Element from an Array in Python

Introduction

This tutorial will go through some common ways for removing elements from Python arrays. Here's a list of all the techniques and methods we'll cover in this article:

Arrays in Python

Arrays and lists are not the same thing in Python. Although lists are more commonly used than arrays, the latter still have their use cases. The main difference between the two is that lists can be used to store arbitrary values. They are also heterogeneous which means they can simultaneously store integers, strings, other objects, etc.

Arrays, on the other hand, are similar to what arrays are in C. They are homogeneous data structures for storing elements of the same type and they use much less memory than lists.

This tutorial will focus on arrays, instead of lists, although most of the techniques shown in this tutorial can be used on both of these two data structures.

Using remove()

Appropriately, the remove() function can be used on any array 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 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 array elements. 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 technically also arrays, and since they are commonly used (especially in machine learning), 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 array 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 an array 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.

Author image
About Luka Čupić
Croatia Website