How to Remove Elements from an Array in Python?

In this tutorial, I will explain how to remove elements from an array in Python. As a data scientist working for a US-based company, I recently encountered a situation where I needed to remove specific elements from an array. In this article, I will share the different methods, to achieve this, along with practical examples and screenshots of executed example codes.

Arrays in Python

Before we remove elements from an array, let us discuss what are Python arrays. In Python, arrays are a collection of elements of the same data type, stored in contiguous memory locations. Python provides the array module to work with arrays.

If you want to remove the first element from the array you can refer to this tutorial How to Remove the First Element from an Array in Python

Removing Elements from a Python Array

Python provides various ways to remove elements from an array in Python, Let us see some important methods.

Method 1. Use the remove() Method

One of the most simple ways to remove an element from an array in Python is by using the remove() method. This method removes the first occurrence of the specified element from the array. Here’s an example:

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
cities.remove("Chicago")
print(cities)

Output:

["New York", "Los Angeles", "Houston", "Phoenix"]

I have executed the above example code, and you can see it in the below screenshot.

Remove Elements from an Array in Python

In this example, we have an array cities containing names of major US cities. We use the remove() method to remove the city “Chicago” from the array. The remove() method modifies the original array in place.

Read How to Convert Python Dict to Array

Method 2. Use the pop() Method

Another way to remove an element from an array is by using the pop() method. By default, it removes and returns the last element of the array. However, you can also specify the index of the element you want to remove. Here’s an example:

states = ["California", "Texas", "Florida", "New York", "Pennsylvania"]
removed_state = states.pop(2)
print(removed_state)
print(states)

Output:

Florida
["California", "Texas", "New York", "Pennsylvania"]

I have executed the above example code, and you can see it in the below screenshot.

How to Remove Elements from an Array in Python

In this example, we have an array states containing names of US states. We use the pop() method with an index of 2 to remove and return the element at that index, which is “Florida”. This method modifies the original array in place and returns the removed element.

Check out How to Print Duplicate Elements in Array in Python

Method 3. Use the del Keyword

Python also provides the del keyword, which allows you to remove elements from an array by specifying their index. You can also use del to remove a range of elements. Here’s an example:

presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe"]
del presidents[1:3]
print(presidents)

Output:

["George Washington", "James Madison", "James Monroe"]

I have executed the above example code, and you can see it in the below screenshot.

Remove Elements from an Array in Python Use the del Keyword

In this example, we have an array of presidents containing names of US presidents. We use the del keyword followed by the array name and a slice [1:3] to remove elements from index 1 to 2. The del keyword modifies the original array in place

Read How to Update an Array in Python

Method 4. Use the delete() Method

If you’re working with NumPy arrays, you can use the delete() method to remove elements. This method takes the index of the element to be removed as an argument. Here’s an example:

import numpy as np

temperatures = np.array([72, 68, 75, 80, 65])
temperatures = np.delete(temperatures, 2)
print(temperatures)

Output:

[72 68 80 65]

In this example, we create a NumPy array called temperatures containing temperature values in Fahrenheit. We use the delete() method to remove the element at index 2, which is 75. This method returns a new array with the specified element removed.

Read Python repeat array n times

Method 5. Use List Comprehension

List comprehension is an efficient way to create new lists based on existing lists. You can also use list comprehension to remove elements from an array based on a condition. Here’s an example:

scores = [85, 92, 78, 90, 88, 82, 95]
passing_scores = [score for score in scores if score >= 85]
print(passing_scores)

Output:

[85, 92, 90, 88, 95]

In this example, we have an array scores containing the test scores of students. We use list comprehension to create a new array called passing_scores that only includes scores greater than or equal to 85. This effectively removes the scores that do not meet the passing criteria.

Check out How to Get Values from a JSON Array in Python

Best Practices

When removing elements from an array in Python, consider the following best practices:

  • Use the remove() method when you know the element you want to remove.
  • Use the pop() method when you want to remove and return an element at a specific index.
  • Use the del keyword when you want to remove elements based on their index or a range of indices.
  • Use the delete() method when working with NumPy arrays.
  • Use list comprehension when you want to remove elements based on a condition and create a new array.

Read How to Convert an Array to a Tuple in Python

Conclusion

In this tutorial, I explained various methods to remove elements from an array in Python. We discussed the remove() method, pop() method, del keyword, delete() method for NumPy arrays, and using list comprehensions.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.