Intro to Python

13. Arrays

So far, we only know how to store data in variables, which store a single piece of data and give the data a name that we can later reference. But what happens if we want to store large amounts of data (e.g., user accounts, leaderboards, text messages, etc.)? Assigning variables for every piece of data would be impractical and impossible, especially for massive tech companies like Google, where thousands of data points are generated every second. In these cases, we need to use a data structure, a programming structure designed to hold data. The difference between data structures and variables is that data structures can contain many pieces of data at once and, depending on how the data structure is formatted, enable the programmer to manipulate the data effectively.

An array is the most basic and widely used data structure designed to hold a fixed number of data points (called the array size) of the same type in contiguous memory locations. The fixed number and data type need to be provided when the array is created, but the data itself can be manipulated and modified later, which is what makes an array powerful.

Python does not support arrays and instead uses a data structure called a list that functions very similarly. We will use the words arrays and lists interchangeably in this lesson.

Creating Lists

How do you create a list? Just put some square brackets around a bunch of comma-separated elements.

For example, if we wanted to store a shopping list, we could write

grocerylist = ["milk", "bananas", "eggs", "flour", "bread", "watermelon", "butter", "apples", "bagels"]

We can initialize an empty list of length 10 with the following code.

# creates empty list of strings of length 10
groceryList = [""] * 10

Accessing and Modifying Lists

An array can be treated as a sequence of variables (called elements) of the provided data type, but instead of accessing each variable by name, they are accessed using the array’s name with an index. Python is zero-indexed, meaning arrays start with index 0.

With this scheme, the first element in an array is accessed with the syntax arr[0], the 2nd element is accessed with arr[1], the 3rd element is accessed with arr[2], and so on.

Once you have accessed an element in your array, you can manipulate it as if it was a normal variable of the array’s data type. For example:

  • We can modify it:
# initializing list of integers of length 10
arr = [0] * 10

# sets the first element to 50
arr[0] = 50

print(arr[0])
> 50
  • We can add them (and use all of the other arithmetic operators):
# initializing list of integers of length 10
arr = [0] * 10

# sets the first element to 3, sets second element to 2
arr[0] = 3
arr[1] = 2

# adds elements
print(arr[0]+arr[1])
> 5
  • We can use it in its own modification equation:
# initializing list of integers of length 10
arr = [0] * 10

# sets the first element to 5
arr[0] = 5

# adds 2 to first element
arr[0] = arr[0] + 2

print(arr[0])
> 7

One of the most important uses of arrays is in conjunction with loops. Recall that for-loops enable you to run the same block of code multiple times with an iterator variable. We can use that to perform an operation on every element of an array. For example, we could double the value of every element:

arr = [0] * 10
arr[0] = 4;
arr[1] = 2;
arr[2] = 0;
arr[3] = 5;
arr[4] = 7;
arr[5] = 11;
arr[6] = 6;
arr[7] = 3;
arr[8] = 1;
arr[9] = 9;
for i in range (10):
    arr[i] = arr[i] * 2
    print(arr[i])
> 8
> 4
> 0
> 10
> 14
> 22
> 12
> 6
> 2
> 18

A more helpful operation that a programmer might see in the real world could be connecting two String arrays. In this example, we combine two String arrays consisting of first and last names to get an array of full names.

firstNames = ["Susan", "John", "Tracy"]
lastNames = ["Milton", "Smith", "Chang"]
fullNames = [""] * 3

for i in range (3):
    fullNames[i] = firstNames[i] + " " + lastNames[i]
    print(fullNames[i])

> Susan Milton
> John Smith
> Tracy Chang

2D Lists

Much like nested for-loops and if-statements, we can nest arrays inside one another by making an array where the data type is another array. This is called a 2D array.

In Python, we can initialize a 2D list using the code below.

# cols = number of columns
# rows = number of rows
arr = [[0 for i in range(cols)] for j in range(rows)]

For example, if we wanted to create a 2D list with 3 rows and 4 columns, we could write

arr = [[0 for i in range(4)] for j in range(3)]

In our original 1D array, we used an index to access certain elements inside the array. This also works for our 2D array. Using a single index allows us to access an element inside the array. However, because in 2D arrays the data type of the outer array is another array, the element we access using a single index is just an inner array. So, we need to go one step deeper using a second index to access an element of the inner array. This code accesses the element located at index 3 of the inner array located at index 0 of the outer array.

arr[0][3]

Once you have used two indices, every element of an inner array can be manipulated like a variable just like with our normal arrays:

arr = [[0 for i in range(12)] for j in range(10)]
arr[5][2] = 56
print(arr[5][2])
> 56

2D arrays are visually represented using a 2D grid where the rows are each of the inner arrays. This, for example, is a depiction of a 3x3 2D array. There are 3 arrays of size 3, one in each row.

Note: If you are using multi-dimensional arrays, be careful with declaring too many dimensions. Every additional dimension increases the memory required to store the array exponentially.

You can play with all the code we've used in this article on Trinket: