What are Arrays/Lists and Why Should I Care About Them?
This lesson will answer the question of what an array/list is and why they're important for developers. It will give a basic explanation for the array data structure and show how it can be used.
Guide Tasks
  • Read Tutorial

If you're an experienced developer and you've been working with the array or list data structure for a number of years and you can skip over this section. However if you're relatively new to programming this will be an important introduction to this collection data type. The majority of languages call this collection type an array, however Python calls its a list. For the sake of simplicity I'll be calling it an array for the rest of this course.

I remember when I was originally learning development. Since I learned how to build web applications before I learned computer science my first reaction to arrays was why are these important? My young self had gotten so used to simply working with database queries that I didn't realize that I had been working with arrays for years and hadn't really thought about them.

What is an Array

Let's begin with a dead simple definition to answer the question What is an Array?

An array is a collection of objects.

Is that really it? Obviously arrays can be much more complex than this basic definition, but at their core they are essentially a container that can store objects.

Arrays behave slightly differently depending on the programming language that you're working with. For example, not all languages allow you to store multiple data types in a single array. We'll go through a demo of the different types shortly.

If the high level concept of arrays is still a little fuzzy, let's review a real world example. Imagine a baseball team.

large

A team is a single entity, just like an array. But it's made up of multiple team members, just like an array has multiple elements stored inside it.

Additionally, you can remove a player from a team, add a new player, re-arrange the batting order, etc. In the same way arrays have many different functions that allow you to work with the data inside of them.

Why Should I care About Arrays?

Arrays are used so often in development that I can honestly say that if you don't master arrays you won't be able to work on programs of any significance. Thankfully arrays are relatively straightforward to understand and work with after a little practice.

In our Practical Application section we'll take a look at few real world examples of how arrays can be used in projects.

Practical Application

Extending our baseball example from earlier this lesson, what if we were asked to build a batting lineup program for a team? The most natural data structure choice would be to use an array to store the data. Even if the team names were in a database, we would still need to store them in an array after the database was queried. There are a number of ways that we can structure an array, I'll start with the most straightforward example, where I'm simply storing their names.

team = [
  "George Springer",
  "Jose Altuve",
  "Carlos Correa",
  "Evan Gattis",
  "Tyler White",
  "Alex Bregman"
]

Using the Python programming language I've created an array called team that contains 6 different elements, each representing a team member. Why wouldn't I just store each team member in a variable? Well, mainly because when we use arrays we're able to take advantage of all of the methods available to the data structure. This can include features such as: looping, adding new elements dynamically, removing elements, and much more. Performing these types of actions wouldn't be scalable simply by using variables.

We can also extend the complexity of arrays by allowing them to store nested arrays. For example, what if we wanted our team array to contain the position values (I'm changing the name of the variable holding the data to make it more explicit, however that isn't required for this to work):

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

Our array still contains 6 elements. However now the elements are actually arrays themselves. In the next few lessons we'll walk through how to use arrays, including how to select elements, remove items, and add items to the data structure.