Finding an Element in an Array
This guide walks through how to find an element in an Array using various data structure query tools and the Python programming language.
Guide Tasks
  • Read Tutorial

This will be a relatively short guide since the concept is relatively straightforward. When working with the array/list data structure there are many times when you'll need to be able to find an element in the array. And that's what we'll be walking through in this lesson.

We'll start out with a basic example and then walk through a slightly more complex use case.

Imagining back to our baseball team program you'll remember that we have a data structure that contains players and position values for a baseball team.

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

This array is all well and good, but it's pretty pointless if we can't access any elements inside of the array. Let's walk through how to find and select elements in array. But before we get started, we first need to understand a very important feature of arrays, the index.

Arrays Start at 0

Depending on your experience with arrays, it may surprise you to learn that arrays have an internal counting mechanism called the index. This index keeps track of where every element in the array is located. For example, if our values for the baseball team array would be replaced with their associated index values, it would look something like this:

players_and_positions = [
  0,
  1,
  2,
  3,
  4,
  5
]

Did you happen to catch the oddity with the index values? If you noticed that they start at 0 you get a gold star. This is a very important topic because it will relate to many many different algorithms and how they're implemented. For example, when you're attempting to count the total number of items in an array you can't simply use the final index value, you need the final index value + 1 to get the total.

I also wanted to take a quick break to mention this counting mechanism because you'll need it in order to select values in an array.

Selecting Values in an Array

In order to select an element from an array the syntax is pretty straightforward. The formula is:

  1. Supply the name of the array
  2. Give the integer value of the index

So let's open up the code and try to select ["Carlos Correa", "SS"] from the array:

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

print(players_and_positions[2])
#=> ["Carlos Correa", "SS"]

In this code we called the players_and_positions array and asked it to return the element associated with the index of 2 and it worked properly.

What would happen if we try to call an index value that doesn't exist? Let's find out.

print(players_and_positions[200])
#=> IndexError: list index out of range

Looking for an item with index 200 (which doesn't exist) will return an IndexError, since no value is there. This behavior will vary based on the programming language that you're working with. However the key item to remember is that you can only select items in array with a proper index value.

Selecting Nested Array Elements

Our last example is great, but it seems a little half hearted. For example, our code players_and_positions[2] simply returned another array. What happens when we need to grab the value inside of the nested array? This is actually a pretty straightforward process, let's run the code:

print(players_and_positions[2][0])
#=> "Carlos Correa"

With arrays you're able to chain index lookups right next to each other. To break this down, let's look at players_and_positions[2][0] on a component basis:

  • players_and_positions is the name of the array
  • [2] selects the 3rd element in the array, in this case it's the array containing ["Carlos Correa", "SS"]
  • Lastly the [0] is grabbing the first element of the nested ["Carlos Correa", "SS"] array, which returns the value "Carlos Correa"

Summary

I hope that this has been a helpful guide for understanding how to grab elements from an array, including a walk through on first level and nested array elements using the bracket syntax.

What's Next?

In the next guide we'll explore how we can add elements to an array.