Tutelage

Lessons › coding and computer science · taught by Leo

Lists and arrays explained: indexing basics

Learn how lists and arrays store ordered values, how zero-based indexing works, and how to read items by index.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Why lists store many values

Think of a list as a row of labeled boxes. Each box holds one thing.

The important part is order. The first box is not the same as the second box.

In many languages, arrays are a kind of list made for storing many values together.

List vs array in one sentence

A list is a collection of items in order. An array is a list-like structure that often stores items in a fixed-size, efficient way.

Use list when you mean a group of values.

Use array when you want to talk about indexed storage, especially in coding.

Different languages use these words a little differently, so the exact meaning can change.

Predict the next index

If the first item is at index 0, what is the index of the third item?

  1. 1
  2. 2
  3. 3
Show answer

2: Yes. Count from zero, so the third item is at index two.

Read an item by index

Here is a simple list. I am using square brackets to show the items.

To get one item, write the name and the index in brackets.

Because indexing starts at zero, index one gives the second value.

Quick check on indexing

In [10, 20, 30, 40], what does arr[2] give?

  1. 10
  2. 20
  3. 30
Show answer

30: Correct. Index two points to the third item, which is 30.

Why loops love arrays

Arrays are useful because you can visit every item one by one.

A loop usually starts at the first index and moves forward.

That makes it easy to total numbers, search for something, or print everything.

Why indexing works

The index is like an address for each slot. That is why the program can jump straight to the item you want.

The index is not the value. It is the location inside the list.

That is why arr[2] means 'give me the item at spot 2'.

This is the core idea behind random access.

Watch out for off-by-one

If a list is [5, 8, 11], which index gets 8?

  1. 0
  2. 1
  3. 2
Show answer

1: Right. 8 is the second item, so its index is one.

Do not mix up value and index

This is the most common confusion. The index tells where, and the value is what lives there.

If you keep those two ideas separate, array problems get much easier.

In code, brackets point to the place, not the meaning of the number.

Your turn with a new list

Given vals = [3, 6, 9, 12], what is vals[3]?

  1. 9
  2. 12
  3. 3
Show answer

12: Nice. Index three means the fourth item, which is 12.

Memory checklist for lists

Here is the checklist. Say it to yourself before you answer array questions.

First, remember order. Second, remember zero-based indexing.

Third, brackets select one item, and the index is not the value.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app