Deleting Elements from an Array
This lesson on algorithms walks through the process of deleting elements from an array, including a code example in the Python programming language.
Guide Tasks
  • Read Tutorial

Since you what arrays are, how to find elements in an array, and how to add items to an array the next step will becoming familiar with removing items from an array.

Staying with our example of building a baseball team management application, the time will come when players have to be removed. Thankfully when it comes to implementing algorithms, the process for removing items from an array is very similar to how we add items.

Every language has a slight different syntax for removing items from an array. When it comes to working with languages like Scala, some of their array structures don't allow you to change them at all, the formal term for this type of behavior is immutable.

We're going to keep using the same array data structure that we've been using in this section:

players_and_positions = [
  ["George Springer", "OF"],
  ["Jose Altuve", "2B"],
  ["Carlos Correa", "SS"],
  ["Evan Gattis", "DH"],
  ["Tyler White", "1b"],
  ["Alex Bregman", "3B"]
]

Manually removing items

To manually remove an item we can remove it by index value with the del method:

del players_and_positions[2]

This will remove the third item in the data structure.

Or you can remove an item by it's value:

players_and_positions.remove(["Tyler White", "1b"])

This removes the specific item that we passed to the remove method.

If you're wondering if you can remove nested values, the answer is yes! Let's remove the position from one of the nested arrays:

del players_and_positions[0][1]

Here we're first selecting the element at index 0 and deleting the position, which is at index value 1. If you run this program you'll see that the position's player has been deleted.

Automated way to remove items from an array

It you don't know the index or the value that you want to remove you can use the pop method. The pop() method will remove the last item from the array, so this should only be used when you want that item deleted.

Updating our code as follows:

players_and_positions.pop()

Will remove the last item from the array, this one is an easy one to remember if you think about the array as a tower and you're popping the top item from the structure.

Don't forget the return

Before we end this lesson there is a very important process that's happening when we remove items from an array. When we're working with algorithms it's pretty rare that we simply want to remove an item from an array. Instead we want to remove it and store it in a variable to use in the program.

For example, let's say that one feature of our baseball program was a mobile app that used by the umpires to track who was at the plate. We wouldn't want to actually delete the element, we'd want to store it in a variable, like this:

at_bat = players_and_positions.pop()

print(last_player)
# => ['Alex Bregman', '3B']

Notice that the pop method doesn't only remove the element from the array, it also returns it so that it can be stored in a variable, as shown here when we print out the at_bat value.

Keep this piece of functionality in the back of your mind. We'll be using this in a number of key algorithms so it will be an important feature to remember.

What's Next?

Next we're going to go through the abstract Queue data structure.

Resources