University of Free Knowledge
QA 76.73 · fol. 13

An Ordered, Changeable Collection

A list is an ordered, index-addressed, mutable collection of values you can read, grow, change, and loop across. · 11 min

So far every name has held one value at a time. But a program often needs many values kept together — the scores in a game, the words in a line, the prices on a receipt. A list gives all of them one name and keeps them in order. Once you have that one name, you can read any value in the list, add more, change what is stored, and walk across every value with a loop. This folio is those four powers, one at a time.

Guess before you learn

A list is written in square brackets: `` nums = [10, 20, 30] ` What does nums[1]` give you?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

A list is an ordered, mutable sequence of values addressed by index. Indexing is zero-based: in nums = [4, 8, 15], nums[0] is the first value and nums[len(nums) - 1] the last; a negative index counts back from the end, so nums[-1] is 15. Because a list is mutable, you can rebind a slot with nums[1] = 99, grow it with nums.append(23), or shrink it — all in place, under the same name. Anything a for loop does over range() it can do over a list, binding the loop variable to each value in order. Read, grow, change, and loop: those four powers are the whole of basic list work.

index

The position number of a value in a list, counting from 0. In [4, 8, 15], the value 15 is at index 2.

INDEX0123value'a''b''c''d'from the end-4-3-2-1
PLATE I Every value sits at a position: count from 0 at the front, or from -1 at the back.

You read a single value by writing the list's name and the index in square brackets. nums[0] is the first value, nums[2] is the third. A negative index counts from the back, so nums[-1] is always the last value however long the list is. And len(nums) reports how many values the list holds. Notice the gap this opens: a list of three values has slots numbered 0, 1, and 2, so its length is 3 but its last index is only 2.

Read values out of nums = [4, 8, 15, 16] — the steps fade as you master them

1
Indexing starts at 0. What is nums[0], the first value?
nums[0]
2
Count 0, 1, 2. What is nums[2]?
nums[2]
3
A negative index counts from the end. What is nums[-1], the last value?
nums[-1]
4
len counts how many values there are. What is len(nums)?
len(nums)
Retrieval Gate — answer before you continue 0 / 4

1.Given colors = ['red', 'green', 'blue'], what is colors[0]?

2.For nums = [5, 10, 15, 20], what is nums[2]?

3.What is len([3, 1, 4, 1, 5])?

4.For nums = [8, 6, 7], what does nums[-1] give?

The second power is change. A list is mutable, which means you can alter what it holds without making a new one. Assign into a slot to replace one value: nums[0] = 99 swaps whatever was first for 99. Add a value to the end with nums.append(23), which makes the list one longer. Both act on the same list under the same name — you are editing it in place, not building a copy. This is the trait that separates a list from the fixed values you have used until now.

Why is this true?

Why does a list of 3 values have no slot numbered 3?

Because indexing starts at 0, the three slots are numbered 0, 1, and 2. The length is 3, but the last valid index is 2 — asking for index 3 runs off the end and raises an IndexError.

OPERATIONEXAMPLERESULTread a slotnums[0]4last valuenums[-1]15how manylen(nums)3change a slotnums[1] = 99[4, 99, 15]add to the endnums.append(23)[4, 8, 15, 23]
PLATE II The everyday list operations, each shown on the original nums = [4, 8, 15].

The fourth power is the loop. A for loop can walk a list directly: for score in scores: runs its body once for each value, binding the loop name to that value in turn — first scores[0], then scores[1], on to the end. You do not manage an index yourself; the loop hands you the values one at a time, in order. This is why lists and loops arrive together: a loop is how you do something to every value a list holds.

Ink That Thinks — guess first; the answer draws itself.
You start with an empty list and append one value on each pass of a loop. Place a point for the list's length after 0, 1, 2, 3, and 4 appends — commit your guesses in pencil first.

012345012345appends donelen(list)
Tap to place each point.
PLATE III One append, one more value — guess in graphite, truth in ink.
Retrieval Gate — answer before you continue 0 / 4

1.What does this print? `` nums = [1, 2, 3] nums[0] = 9 print(nums) ``

2.What does this print? `` nums = [1, 2, 3] nums.append(4) print(len(nums)) ``

3.In for x in [10, 20, 30]:, what does x hold on each pass?

4.In one sentence, what does it mean that a list is mutable?

That is the whole of a list: one name for many values, kept in order, each reachable by index, all of it changeable, and every value visitable with a loop. Read, grow, change, loop. Next folio looks at a sequence you have quietly used since the first program — the string — which is indexed just like a list but, unlike a list, cannot be changed once made.

Practice — new ink and old, interleaved

1.What does this print? `` a = 3 b = a a = 10 print(b) ``

2.What does this print? `` total = 0 for x in [1, 2, 3, 4]: total = total + x print(total) ``

3.For nums = [2, 4, 6, 8], what is nums[3]?

4.Review from folio 1: in what order does the computer run a program's statements?

5.What is nums after this runs? `` nums = [1, 2] nums.append(5) ``

6.Put these lines in the correct order to classify a number as negative, zero, or positive.

  1. if n < 0: print('negative')
  2. elif n == 0: print('zero')
  3. else: print('positive')

7.Without looking back: in an if / elif / else chain, which branch runs, and what does else do?

8.What is the last value i takes in for i in range(2, 6):?

9.What prints? `` x = 7 if x > 5: print('big') else: print('small') ``

10.How many times does the body run? `` i = 0 while i < 3: i = i + 1 ``

The Call Slip — search everything Ctrl·K / ⌘K