Skip to main content
CodeFlow
data structures~12 minRoute 02 1 of 9

Arrays & Lists

A numbered row of slots — the data structure underneath almost everything else.

Prerequisites:Variables

Story — a row of numbered lockers

0a1b2c3d4ethe number IS the address

Picture a long row of identical lockers, each one numbered: 0, 1, 2, 3… Anyone walking by can read the number above a locker and look inside instantly. No searching for a name — the number is the address.

That row of lockers is an array (or, when it can grow on demand, a list). Each slot holds one value; each slot has a numbered index; reading any slot by index is instant. Almost every other data structure is built on top of this one.

Indexa says

Slot 0 is the first slot — not slot 1. That tripped up everyone the first time. Once you accept it, it stops being weird.

See it — append, insert, index

Click any slot to read it. Append adds to the end (fast). Insert at 0 pushes everything right (slow on big arrays). Watch the slot numbers shift when you insert at the front.

A 4-element array. Slot 0 holds "a".

length = 4

✎ Sharpen your pencil

Watch what happens to indexes when you Insert at 0 a few times. In one sentence, why is that operation slow on a big array?

Indexa says

Append is cheap, insert-at-zero is expensive. Always ask: am I touching the end, or the front?

Try it — predict the index

Exercise. Start with arr = ["a", "b", "c"]. Append "d". Insert "z" at the front. What is arr[2] now?

Reveal answer

After append: ["a", "b", "c", "d"]. After insert at 0: ["z", "a", "b", "c", "d"]. So arr[2] is "b" — every element shifted right when "z" arrived.

Code it — five languages, one row

Subscript with brackets, append with the language's growth method (append, push, add, push_back), ask for length. The shape is universal.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])       # "apple"
fruits.append("date")  # → 4 items
print(len(fruits))     # 4

Walk through it line by line

Step 1 / 4python
1fruits = ["apple", "banana", "cherry"]
2fruits.append("date")
3first = fruits[0]
4last = fruits[-1]

Quiz it — make it stick

  1. Question 1

    In most languages, what is the index of the first element of an array?

  2. Predict — Question 2

    What does this print?

    nums = [10, 20, 30]
    nums.append(40)
    print(nums[1] + nums[3])
  3. True or false — Question 3

    Inserting at the front of a 1,000,000-element array is just as fast as appending to the end.

No Dumb Questions

Real questions other learners asked on this page.

  • What's the difference between an array and a list?
    In casual talk we use them interchangeably. Strictly: a fixed-size array (Java int[], C int[10]) cannot grow. A "dynamic array" or "list" (Python list, JS Array, Java ArrayList, C++ vector, Go slice) hides a fixed array under the hood and reallocates when full. Most beginner tutorials say "array" but really mean dynamic array.
  • Why does indexing start at 0?
    Because under the hood, arr[i] computes "start of array + i × size of element." For the first element you want the offset to be zero, so i = 0. Edsger Dijkstra wrote a famous note (EWD831) explaining why 0-based indexing makes ranges and loops cleaner.
  • What happens if I read past the end?
    Depends on the language. Python and Java raise an exception. C++ is undefined behaviour with [] but throws with .at(). Rust panics. JavaScript returns undefined. Always check or use a language with safe indexing — out-of-bounds reads are a classic source of security bugs.