Words carry meaning and meaning is important in computer science.
If you asked the question, your words can carry either of the two meanings. In other words, there are two ways to read this question:
- How to remove an element from a list in Python?
- How to remove an element from a NumPy array in Python?
The reason is that Python doesn’t have a built-in array data type like in other programming languages such as C++ or Java.
Python has lists. Lists are similar to array in that accessing or removing the i-th element in a Python list has constant runtime complexity.
Okay, let’s get rid of the nerd talk and solve the problem (1.) first:
How to remove an element from a list in Python?
Lists are ordered data structures so they store elements in a given order.
You can call the method list.pop(index)
to remove the element at position index
. If you don’t provide an index by calling list.pop()
, Python simply removes the last element.
Here’s an example:
my_list = ["Alice", "Bob", "Carl"] my_list.pop(1) print(my_list) # ['Alice', 'Carl']
The code snippet creates a list of three string elements and removes the second element via list.pop(1)
.
π Note: Python has zero-based indexing, i.e., the index of the first element is 0, and the index of the i
-th element is (i-1)
. This is a common source of bugs!
In case you need more ways to remove an element from a list, you can check out our detailed guide here:
Feel free to also watch our video tutorial here:
Okay, so let’s explore the second way to interpret your question:
How to Remove an Element from an Array in Python?
If you want an array, chances are you’re seeking a NumPy array.
How to remove an element from a NumPy array?
NumPy is Python’s defacto standard library for numerical computations.
A NumPy array can have one or more dimensions.
- If it has one dimension, we may call it a vector.
- If it has two dimensions, we may call it a matrix.
- If it has n dimensions, we may call it an n-dimensional matrix.
In this article, we’re going to explore the one-dimensional case:
How to remove an element from a one-dimensional NumPy array?
To remove an element at a given index
from a 1D NumPy array
, call the function np.delete(array, index)
that returns a new array with the element removed.
Formally, the method has the following syntax:
numpy.delete(arr, index_or_object, axis=None)
Here’s a simple example that removes the second, fourth, and sixth elements (with indices [1, 3, 5]
) from the original NumPy array:
import numpy as np # Original NumPy array a = np.array([10, 20, 30, 40, 50, 60, 70]) # Indices to be removed index = [1, 3, 5] # New array generated result = np.delete(a, index) # Output print(result) # [10 30 50 70]
The resulting array has the specified indices removed.
β Note: If you only want to remove a single element from a given index, pass only a single integer as index.
Here’s an example:
import numpy as np # Original NumPy array a = np.array([10, 20, 30, 40, 50, 60, 70]) # Indices to be removed index = 3 # New array generated result = np.delete(a, index) # Output print(result) # [10 20 30 50 60 70]
The result shows that only the fourth element with index 3
has been removed.
I just found this related video you may enjoy:
Okay, let’s wrap this up!
Summary
To summarize, there are two ways to answer your question:
- To remove the element at position
index
, call the methodlist.pop(index)
. - To remove an
element
from a 1D NumPyarray
, call the functionnp.delete(array, element)
that returns a new array with the element at the specified index removed.
Thanks for spending your valuable time with us. Feel free to join our email academy to keep improving your Python skills day by day: