What is an array?
An array is a row of items stored one after another in memory. That layout makes reading any item very fast and makes inserting an item in the middle slow.
A compiler could keep the line numbers of its warnings in such a row. Adding a new warning at the right place means moving every later number along by one.
By the end of this lesson
Note
I can explain how a list is stored and what inserting an item costs.
Key words
| Word | What it means | Example |
|---|---|---|
| Array | items of one size stored side by side in memory | |
| Element | one item in an array | |
| Index | the position of an element, counted from 0 | items[2] |
| Contiguous | with no gaps between the items | |
| Shift | move an element one place along to make or close a gap | |
| Capacity | how many elements the stored block has room for |
The cells students fill in are left blank in this preview.
An array stores items side by side
Memory is a long row of numbered boxes. Each box has an address. An array takes a run of boxes with no gaps, and every element is the same size.
So the computer needs no search to find one. It works out the address: the start, plus the index times the size of one element.
Worked example: where each element lives
| Index | Calculation | Address |
|---|---|---|
| 0 | 1000 + 0 x 4 | 1000 |
| 1 | 1000 + 1 x 4 | 1004 |
| 2 | 1000 + 2 x 4 | 1008 |
| 3 | 1000 + 3 x 4 | 1012 |
The cells students fill in are left blank in this preview.
The size of one whole-number element
python3 -c "import array; a = array.array('i', [10, 20, 30]); print(a.itemsize, len(a))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Whole numbers against decimals
python3 -c "import array; print(array.array('i').itemsize, array.array('d').itemsize)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Inserting in the middle shifts every later item
There are no gaps, so a new element cannot just be squeezed in. Every element after the chosen place must move one box along, starting from the last one.
Deleting is the reverse: the later elements move back to close the gap. Adding at the very end moves nothing, so it is cheap.
Worked example: insert 15 at index 1
| Step | What happens | Row afterwards |
|---|---|---|
| Start | Four elements | 10 20 30 40 |
| 1 | Make room at the end | 10 20 30 40 _ |
| 2 | Move 40 along | 10 20 30 _ 40 |
| 3 | Move 30 along | 10 20 _ 30 40 |
| 4 | Move 20 along | 10 _ 20 30 40 |
| 5 | Write 15 in the gap | 10 15 20 30 40 |
The cells students fill in are left blank in this preview.
Count the shifts yourself
# Insert 15 at position 1 of a row of four, counting every shift.
items = [10, 20, 30, 40]
shifts = 0
items.append(0)
i = len(items) - 1
while i > 1:
items[i] = items[i - 1]
shifts = shifts + 1
i = i - 1
items[1] = 15
print(items, shifts)
# Change both 1s (in "i > 1" and "items[1]") to 0 and run again.
Insert into the middle of a list
python3 -c "a = [10, 20, 30]; a.insert(1, 15); print(a)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Remove the first element
python3 -c "a = [10, 20, 30, 40]; a.pop(0); print(a)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think adding to the front of a list costs the same as adding to the end. It does not. Adding at the end moves nothing, but adding at the front shifts every element. With a million elements, that is a million moves for one insert.
A full array is copied
A block of memory cannot always grow in place, because the boxes after it may be in use. When a list is full, Python asks for a bigger block, copies every element across and frees the old one.
It asks for more room than it needs, often about double, so most appends find a free space waiting.
Worked example: appending past capacity 4
| Append number | Elements after | Capacity | Copy needed |
|---|---|---|---|
| 1 to 4 | 4 | 4 | No |
| 5 | 5 | 8 | Yes, 4 elements copied |
| 6 to 8 | 8 | 8 | No |
| 9 | 9 | 16 | Yes, 8 elements copied |
The cells students fill in are left blank in this preview.