Memory as a row of numbered boxes
🌍 In real life. At Kamalapur station the luggage lockers stand in one long row, numbered 0, 1, 2, … from the left, every locker the same width. If the row starts 100 cm from the wall and each locker is 40 cm wide, locker 7 is at 100 + 7 × 40 = 380 cm. You walk straight to it: you never need to open lockers 0 to 6 first.
That is exactly how a computer finds an item in an array, and a Python list is built on one.
The idea in one sentence: an array keeps its items in one unbroken block of memory, side by side, each taking the same space, so the place of item i can be worked out instead of searched for.
Its words:
- array: items stored side by side in one block of memory, each taking the same space
- address: the number of one place in memory
- index: an item's position counted from 0; the item sits at
start + index × size - random access: reaching any item in one step by working out its address, without walking from the start
How it works:
- memory is a long row of numbered places (addresses)
- an array starts at some address
start, and each item takessizeplaces - item i is at
start + i × size: one multiply and one add, whatever i is - that is why counting starts at 0: the first item is 0 steps from the start
What it costs: reading or writing items[i] is O(1): the same work for item 0 and item 1,000,000. (A Python list stores a small reference in each place, pointing at the actual value, so every place has the same size even when the values don't.)
def address(start, i, size):
return start + i * size
lockers = ["bag", "box", "trunk", "tiffin", "guitar"]
print(address(100, 3, 40), "cm: locker 3 holds", lockers[3])
print(lockers[0], lockers[-1]) # the first, and the last (counting back from the end)
lockers[1] = "parcel" # writing is O(1) too
print(lockers)Mistakes and edge cases. items[len(items)] is one past the end: IndexError: list index out of range. The last item is items[len(items) - 1], or simply items[-1]. And an empty list has no items[0] at all, so check if items: first.
Quiz
The array starts at address 200 and each item takes 8 places. What does this print?
start, size = 200, 8 print(start + 5 * size)Why is reading
items[999999]as fast as readingitems[0]?- Its place is worked out with one multiply and add, not searched for
- Python remembers which items you read most
- It isn't: later items take longer
What does this print?
items = [10, 20, 30, 40] print(items[-1], items[len(items) - 2])Why do array positions start at 0?
- The position is how many items you skip from the start, and the first skips none
- 0 is easier to type
- Python's first item is always empty
Which line has a mistake?
lockers = ["bag", "box", "tiffin"] last = lockers[len(lockers)] print(last)
Coding tasks
Write
address(start, i, size): where itemiof an array is, if the array starts atstartand every item takessizeplaces.Write
swap_ends(items)in O(1) time: swap the first and the last item in place and give back the list (an empty list stays empty).