Lists: indexing and mutation
Create a list, read and change its items by index, and use append, insert, pop and remove, saying what the list is after each call.
It opens on
Last lesson w[0] = "P" on a string raised a TypeError. For scores = [72, 85, 90, 64], will scores[1] = 99 raise an error too, or change the list?
Why it matters
Almost every program keeps a collection of values, test scores, names, readings, and a list is Python's everyday way to hold one; most list bugs are an index one past the end, or a method whose result was used as if it were the list.
Good to know first
- String indexing and slicing: s[0], s[-1] and an index counted from 0
- String methods and immutability: a string cannot be changed, so a method returns a new one
- Defining and calling functions: a call may return a value, or None
Step by step
Read and change by index
Read list items with positive and negative indexes, change one by assigning to its index, and say what an index past the end does.
scores[0] is the first item, 72, and scores[3] is the last, 64. len(scores) is 4, so the last index is always len(scores) minus 1.
A common mix-up
Scores[4] = 50 adds a fifth item.
In fact: Assigning to an index only replaces an existing cell; a position past the end is an IndexError.
Grow and shrink
Use append, insert, pop and remove, and write the list and the returned value after each call.
a.append(7) adds 7 at the end, so a is [5, 8, 2, 7]. a.insert(1, 6) puts 6 at index 1 and slides the items from index 1 on one place to the right, so a is [5, 6, 8, 2, 7].
the first rows of the trace
A common mix-up
a.append(7) returns the longer list.
In fact: It changes a and returns None; the list is a itself.
Traps and costs
Avoid the three classic list-method traps, a result assigned back to the list, a remove of a missing value, and an insert or pop at the front, and give the cost of each call.
The run printed None, and len(a) raised a TypeError, object of type NoneType has no len. append did add the 1, but then the assignment rebound a to the None it returned, and the list was lost. Write a.append(1) on a line of its own.
what the lines printed
A common mix-up
a list method such as append returns the new list, so a = a.append(1) is fine.
In fact: It returns None, which replaces the list.
Try it
For t = [10, 20, 30, 40, 50], what are t[-2] and t[len(t) - 1]?Answer
40 and 50
Hint: -1 is the last, so -2 is the one before it
For k = [1, 2, 3], what does print(k) show after k.insert(1, 9) and then k.remove(3)?Answer
[1, 9, 2]
Hint: Insert slides 2 and 3 right, then remove takes out the 3
For z = [7, 7, 5], what do print(z) and print(7 in z) show after z.remove(7)?Answer
[7, 5] and True
Hint: Remove takes out only one 7
Line 4 is scores[3] = scores[3] + 6. After it, what does print(scores) show on line 5?Answer
[72, 99, 90, 70], because the right side reads the old 64, adds 6 and stores 70 in cell 3.
The next line is x = a.pop() with no index. What does x get, and what is a afterwards?Answer
x gets the last item, 7, and a is [5, 6, 8, 2].
