Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Arrays and lists in memory

After this lesson: I can explain how a list is stored and what inserting an item costs.

50 min lesson4 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

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

WordWhat it meansExample
Arrayitems of one size stored side by side in memory
Elementone item in an array
Indexthe position of an element, counted from 0items[2]
Contiguouswith no gaps between the items
Shiftmove an element one place along to make or close a gap
Capacityhow 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

IndexCalculationAddress
01000 + 0 x 41000
11000 + 1 x 41004
21000 + 2 x 41008
31000 + 3 x 41012

The cells students fill in are left blank in this preview.

The size of one whole-number element

Command
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

Command
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

StepWhat happensRow afterwards
StartFour elements10 20 30 40
1Make room at the end10 20 30 40 _
2Move 40 along10 20 30 _ 40
3Move 30 along10 20 _ 30 40
4Move 20 along10 _ 20 30 40
5Write 15 in the gap10 15 20 30 40

The cells students fill in are left blank in this preview.

Count the shifts yourself

Code: python
# 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

Command
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

Command
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 numberElements afterCapacityCopy needed
1 to 444No
558Yes, 4 elements copied
6 to 888No
9916Yes, 8 elements copied

The cells students fill in are left blank in this preview.

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.