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?
It gives 20. Counting the positions starts at 0, not 1: nums[0] is 10, nums[1] is 20, nums[2] is 30. If you picked 10, keep that pencil mark — reading the second slot as index 1 is the single most common first stumble, and zero-based counting is exactly what the next section makes automatic.
9–12
3–5
A list holds several values under one name, kept in order. You write them inside square brackets: [4, 8, 15]. Each slot has a position number called an index, and counting starts at 0 — so the value at index 0 is the first one. You can read any slot, change what it holds, or add more values to the end.
6–8
A list is an ordered collection of values stored under a single name, written in square brackets: nums = [4, 8, 15]. Each value sits at a numbered position called an index, counting from 0, so nums[0] is 4 and nums[2] is 15. len(nums) tells you how many values it holds. A list is mutable: you can change a value with nums[0] = 99, add one with nums.append(20), and visit every value with a for loop. The order is kept, so the values stay in the sequence you gave them.
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.
K–2
Line up some cups in a row: first, next, next. A list keeps things in a row like that. You can peek in any cup, add a new cup at the end, or swap what one cup holds.
Undergrad
A Python list is a dynamic array: a growable, ordered sequence offering O(1) indexing and amortized O(1) append. A name bound to a list holds a reference, not a copy, so two names can alias one list and a mutation through either is visible through both — the first place value semantics and reference semantics part ways. Indices are zero-based with negative wrap-around; a slice returns a new list. Mutability is the defining trait: unlike a tuple or a string, a list's contents may be reassigned in place, which is what makes it the default container for accumulation and in-place algorithms.
Postgrad
Treat the list as the canonical mutable sequence abstraction: an ordered collection with positional read, in-place update, and dynamic resize. CPython realizes it as a heap-allocated array of pointers with geometric overallocation, giving amortized constant append against occasional O(n) growth copies. Reference semantics make aliasing and identity (is versus ==) first-class concerns; defensive copying or immutable alternatives — tuples, persistent structures — are the standard mitigations. The read / grow / change / loop vocabulary a beginner learns is exactly the interface — indexing, append, assignment, iteration — that later formalizes as the sequence and mutable-sequence protocols.
index
The position number of a value in a list, counting from 0. In [4, 8, 15], the value 15 is at index 2.
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
nums[0]
nums[2]
nums[-1]
len(nums)
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.
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.
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?
From the top line to the bottom line, one statement at a time, exactly as written.
How close were you? Grade yourself honestly — it sets your review date.
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.
- if n < 0: print('negative')
- elif n == 0: print('zero')
- else: print('positive')
7.Without looking back: in an if / elif / else chain, which branch runs, and what does else do?
The branch under the first test that is True runs, and only that one; else runs when every test above it was False.
How close were you? Grade yourself honestly — it sets your review date.
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
``