Basic

Arrays & lists

Store ordered collections of items and access them by position using indexes.

What you will see

3

About 2 min of reading

Try It

Arrays store ordered items you reach by index, and you can grow or shrink them and nest them into grids. I read by position, add and remove items, then pull a value out of a two-dimensional board.

1

Indexes start at 0, so the first item sits at position 0. I read the first and third items to show how a direct position jump works.

fruits = ["apple", "banana", "cherry"] print(fruits[0], fruits[2])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
apple cherry
If the index is missing or too big, you get an error. Indexes count from 0, not 1.
2

I add one fruit to the end and remove another by its value, then print what is left to show how lists change size.

fruits = ["apple", "banana", "cherry"] fruits.append("date") fruits.remove("banana") print(fruits)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
['apple', 'cherry', 'date']
append is fast, but remove has to search for the value first. Removing from the middle also shifts everything after it.
3

A two-dimensional list stores lists inside lists, which is how grids are held in memory. I read the item in row 0, column 2 to show how grid coordinates work.

board = [[1, 2, 3], [4, 5, 6]] print(board[0][2])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3
The first index is the row and the second is the column. Both counts start at 0, so the last row is not row 3.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Items are stored in order; you access any of them directly by its index or walk through sequentially.
1 Store items in a single ordered collection
↓
2 Give each item an index starting at 0
↓
3 Access by index for a direct jump
↓
4 Or iterate from first to last
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

fruits = ["apple", "banana", "cherry"] sets nothing on screen
print(fruits[0], fruits[2]) apple cherry
Why this works
Two-dimensional arrays

Arrays can hold arrays, which is how grids and tables are stored.

board = [
    [1, 2, 3],
    [4, 5, 6],
]
print(board[0][2])   # 3  (row 0, column 2)

Think of the first index as the row and the second as the column. Games, spreadsheets, and image pixels all use grids like this.

When you have a choice

Arrays vs linked lists

Both hold ordered data, but differently:

  • **Array*: items packed together; instant index access, but resizing is costly.
  • **Linked list*: each item points to the next; easy insert/delete, but you must walk through to find something.

Array:    [A][B][C][D]   -> jump to any index
Linked:   A -> B -> C -> D  -> walk step by step

Choose arrays when you read by index; choose linked lists when you insert and delete a lot.

Examples

Accessing by index

Reading items by position

fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # apple
print(fruits[2])   # cherry

Indexes start at 0, so the first item is at position 0. That off-by-one detail trips everyone up at first.

Why it works: arrays store items next to each other in memory, so any index is a direct jump.

Adding and removing

Changing a list

fruits.append("date")   # add to the end
fruits.remove("banana") # remove by value
print(fruits)

append is fast. remove has to search for the item first, which is slower on big lists.

Watch out: removing from the middle shifts everything after it.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Why data structures matter
Previous Lesson
Why data structures matter
Next Lesson
Stacks & queues
Stacks & queues →