Arrays & Lists
A numbered row of slots — the data structure underneath almost everything else.
Prerequisites:Variables
Story — a row of numbered lockers
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
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
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)) # 4Walk through it line by line
1fruits = ["apple", "banana", "cherry"]2fruits.append("date")3first = fruits[0]4last = fruits[-1]
Quiz it — make it stick
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.