A4 · Lesson 12 · Lists
A whole set of readings in one name
A meter logs a reading every second. A design sweep tries thirty resistor values. A parts bin holds every E12 value from 10 Ω to 1 MΩ. Each is a sequence of values you want to keep together, step through, sort, filter and summarize. Python's list does all of that, and it's also the stepping stone to NumPy arrays, which do the same things a hundred times faster.
Predict
x = [10, 20, 30, 40, 50]. What is x[1:3]?
[20, 30]. Indexes start at 0, so x[1] is 20, and a slice x[a:b] stops before b, like range. The slice has b - a items.
Name it
Making and reading lists
readings = [3.31, 3.29, 3.30, 3.35]
empty = []
| Code | Gives |
|---|---|
x[0], x[-1] | first, last |
x[1:3] | items 1 and 2 |
x[:3], x[-3:] | first three, last three |
x[::2] | every other item |
len(x) | how many |
sum(x), min(x), max(x) | total, smallest, largest |
v in x | whether v is in it |
sorted(x) | a sorted copy |
Changing lists
Lists can be changed in place:
x.append(3.28) # add to the end
x[0] = 3.30 # replace an item
x.pop() # remove and return the last
x.sort() # sort in place
Copies and names. y = x doesn't copy: both names refer to the same list, and changing one changes "both". Use y = x.copy() or y = x[:] for a real copy.
Building lists
The loop-and-append pattern:
squares = []
for k in range(5):
squares.append(k * k)
is so common that Python has a shorthand, the list comprehension:
squares = [k * k for k in range(5)] # [0, 1, 4, 9, 16]
big = [r for r in readings if r > 3.3] # filter
volts = [float(s) for s in "3.3,5,12".split(",")] # convert
Statistics by hand
The mean is sum(x) / len(x). The standard deviation measures the spread:
which in Python is two lines:
m = sum(x) / len(x)
s = (sum((v - m)**2 for v in x) / (len(x) - 1)) ** 0.5
The is for a sample from a larger population; A11 explains why.
Worked example
Generate the E12 values from 10 Ω up to 82 kΩ.
Show the solution
- Use a comprehension with two
forparts. Each decade from to multiplies each of the twelve mantissas.
E12 = [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2]
values = [m * 10**d for d in range(1, 5) for m in E12]
print(len(values), values[0], values[-1]) # 48 10.0 82000.0
- Count them. Four decades of twelve values make 48, from 10 Ω up to 82 kΩ.
Answer one comprehension gives all 48 values, from 10 Ω to 82 kΩ.
Worked example
Find the E12 value closest to 3.6 kΩ.
Show the solution
- Keep the closest value so far. Walk through the list and replace
bestwhenever a value is nearer the target.
target = 3600
best = values[0]
for v in values:
if abs(v - target) < abs(best - target):
best = v
print(best) # 3300.0
- Settle the tie. 3.3 kΩ is 300 Ω away and 3.9 kΩ is 300 Ω away too; the loop keeps the first one it finds when they tie.
- A shorter way. Lesson 15 shows the one-line version with
min.
Answer 3.3 kΩ, which ties with 3.9 kΩ and comes first.
Check
Make a list readings of n = 7 values, where reading k is v0 + d * k (with v0 = 3.3, d = 0.02). Then set mean to their average and last3 to the last three readings.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The list x (made for you) holds noisy readings. Set above to a list of the readings greater than t = 5, and count to how many there are.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set s to the sample standard deviation of the list x (made for you), using the formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Using the E12 list made for you, set best to the E12 value closest to target = 3600 Ω.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter makes x, a list of noisy readings. Set smooth to its w-point moving average (3 points): entry i is the mean of x[i] through x[i + w - 1], so smooth is w - 1 entries shorter than x. Use a loop and slices, without NumPy.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
What does this print?
x = [3, 1, 2]
y = sorted(x)
print(x)Explain why
After y = x with x a list, why does x.append(5) change y too?
y = x doesn't make a new list; it makes y a second name for the same list. There's only one list in memory, so changing it through either name changes what both names see. To get an independent list you have to copy it, with y = x.copy() or y = x[:].
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.