generated from PovertyAction/ipa-python-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11-lists.qmd
More file actions
489 lines (350 loc) · 11.7 KB
/
11-lists.qmd
File metadata and controls
489 lines (350 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
---
title: "Lists"
abstract: |
Explain why programs need collections of values. Write programs that create flat lists, index them, slice them, and modify them through assignment and method calls.
date: last-modified
format:
html: default
# Authors
authors-ipa:
- "[Author Name](https://poverty-action.org/people/author_name)"
# Contributors
contributors:
- "[Contributor Name](https://poverty-action.org/people/contributor_name)"
keywords: ["Python", "Programming", "Tutorial", "Data Science", "Jupyter"]
license: "CC BY 4.0"
---
::: {.callout-note}
## Learning Objectives
- Explain why programs need collections of values.
- Write programs that create flat lists, index them, slice them, and modify them through assignment and method calls.
::: {.callout-tip}
## Key Questions
- How can I store multiple values?
## A list stores many values in a single structure
- Doing calculations with a hundred variables called `pressure_001`, `pressure_002`, etc.,
would be at least as slow as doing them by hand.
- Use a *list* to store many values together.
- Contained within square brackets `[...]`.
- Values separated by commas `,`.
- Use `len` to find out how many values are in a list.
```python
pressures = [0.273, 0.275, 0.277, 0.275, 0.276]
print('pressures:', pressures)
print('length:', len(pressures))
```
```output
pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5
```
## Use an item's index to fetch it from a list
- Just like strings.
```python
print('zeroth item of pressures:', pressures[0])
print('fourth item of pressures:', pressures[4])
```
```output
zeroth item of pressures: 0.273
fourth item of pressures: 0.276
```
## Lists' values can be replaced by assigning to them
- Use an index expression on the left of assignment to replace a value.
```python
pressures[0] = 0.265
print('pressures is now:', pressures)
```
```output
pressures is now: [0.265, 0.275, 0.277, 0.275, 0.276]
```
## Appending items to a list lengthens it
- Use `list_name.append` to add items to the end of a list.
```python
primes = [2, 3, 5]
print('primes is initially:', primes)
primes.append(7)
print('primes has become:', primes)
```
```output
primes is initially: [2, 3, 5]
primes has become: [2, 3, 5, 7]
```
- `append` is a *method* of lists.
- Like a function, but tied to a particular object.
- Use `object_name.method_name` to call methods.
- Deliberately resembles the way we refer to things in a library.
- We will meet other methods of lists as we go along.
- Use `help(list)` for a preview.
- `extend` is similar to `append`, but it allows you to combine two lists. For example:
```python
teen_primes = [11, 13, 17, 19]
middle_aged_primes = [37, 41, 43, 47]
print('primes is currently:', primes)
primes.extend(teen_primes)
print('primes has now become:', primes)
primes.append(middle_aged_primes)
print('primes has finally become:', primes)
```
```output
primes is currently: [2, 3, 5, 7]
primes has now become: [2, 3, 5, 7, 11, 13, 17, 19]
primes has finally become: [2, 3, 5, 7, 11, 13, 17, 19, [37, 41, 43, 47]]
```
Note that while `extend` maintains the "flat" structure of the list, appending a list to a list means
the last element in `primes` will itself be a list, not an integer. Lists can contain values of any
type; therefore, lists of lists are possible.
## Use `del` to remove items from a list entirely
- We use `del list_name[index]` to remove an element from a list (in the example, 9 is not a prime number) and thus shorten it.
- `del` is not a function or a method, but a statement in the language.
```python
primes = [2, 3, 5, 7, 9]
print('primes before removing last item:', primes)
del primes[4]
print('primes after removing last item:', primes)
```
```output
primes before removing last item: [2, 3, 5, 7, 9]
primes after removing last item: [2, 3, 5, 7]
```
## The empty list contains no values
- Use `[]` on its own to represent a list that doesn't contain any values.
- "The zero of lists."
- Helpful as a starting point for collecting values
(which we will see in the [next episode](12-for-loops.md)).
## Lists may contain values of different types
- A single list may contain numbers, strings, and anything else.
```python
goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
```
## Character strings can be indexed like lists
- Get single characters from a character string using indexes in square brackets.
```python
element = 'carbon'
print('zeroth character:', element[0])
print('third character:', element[3])
```
```output
zeroth character: c
third character: b
```
## Character strings are immutable
- Cannot change the characters in a string after it has been created.
- *Immutable*: can't be changed after creation.
- In contrast, lists are *mutable*: they can be modified in place.
- Python considers the string to be a single value with parts,
not a collection of values.
```python
element[0] = 'C'
```
```error
TypeError: 'str' object does not support item assignment
```
- Lists and character strings are both *collections*.
## Indexing beyond the end of the collection is an error
- Python reports an `IndexError` if we attempt to access a value that doesn't exist.
- This is a kind of [runtime error](04-built-in.md).
- Cannot be detected as the code is parsed
because the index might be calculated based on data.
```python
print('99th element of element is:', element[99])
```
```output
IndexError: string index out of range
```
::: {.callout-note}
## Exercise: Fill in the Blanks
Fill in the blanks so that the program below produces the output shown.
```python
values = ____
values.____(1)
values.____(3)
values.____(5)
print('first time:', values)
values = values[____]
print('second time:', values)
```
```output
first time: [1, 3, 5]
second time: [3, 5]
```
::: {.callout-tip collapse="true"}
## Solution
## Solution
```python
values = []
values.append(1)
values.append(3)
values.append(5)
print('first time:', values)
values = values[1:]
print('second time:', values)
```
::: {.callout-note}
## Exercise: How Large is a Slice?
If `start` and `stop` are both non-negative integers,
how long is the list `values[start:stop]`?
::: {.callout-tip collapse="true"}
## Solution
## Solution
The list `values[start:stop]` has up to `stop - start` elements. For example,
`values[1:4]` has the 3 elements `values[1]`, `values[2]`, and `values[3]`.
Why 'up to'? As we saw in [episode 2](02-variables.md),
if `stop` is greater than the total length of the list `values`,
we will still get a list back but it will be shorter than expected.
::: {.callout-note}
## Exercise: From Strings to Lists and Back
Given this:
```python
print('string to list:', list('tin'))
print('list to string:', ''.join(['g', 'o', 'l', 'd']))
```
```output
string to list: ['t', 'i', 'n']
list to string: gold
```
1. What does `list('some string')` do?
2. What does `'-'.join(['x', 'y', 'z'])` generate?
::: {.callout-tip collapse="true"}
## Solution
## Solution
1. [`list('some string')`](https://docs.python.org/3/library/stdtypes.html#list) converts a string into a list containing all of its characters.
2. [`join`](https://docs.python.org/3/library/stdtypes.html#str.join) returns a string that is the *concatenation*
of each string element in the list and adds the separator between each element in the list. This results in
`x-y-z`. The separator between the elements is the string that provides this method.
::: {.callout-note}
## Exercise: Working With the End
What does the following program print?
```python
element = 'helium'
print(element[-1])
```
1. How does Python interpret a negative index?
2. If a list or string has N elements,
what is the most negative index that can safely be used with it,
and what location does that index represent?
3. If `values` is a list, what does `del values[-1]` do?
4. How can you display all elements but the last one without changing `values`?
(Hint: you will need to combine slicing and negative indexing.)
::: {.callout-tip collapse="true"}
## Solution
## Solution
The program prints `m`.
1. Python interprets a negative index as starting from the end (as opposed to
starting from the beginning). The last element is `-1`.
2. The last index that can safely be used with a list of N elements is element
`-N`, which represents the first element.
3. `del values[-1]` removes the last element from the list.
4. `values[:-1]`
::: {.callout-note}
## Exercise: Stepping Through a List
What does the following program print?
```python
element = 'fluorine'
print(element[::2])
print(element[::-1])
```
1. If we write a slice as `low:high:stride`, what does `stride` do?
2. What expression would select all of the even-numbered items from a collection?
::: {.callout-tip collapse="true"}
## Solution
## Solution
The program prints
```python
furn
eniroulf
```
1. `stride` is the step size of the slice.
2. The slice `1::2` selects all even-numbered items from a collection: it starts
with element `1` (which is the second element, since indexing starts at `0`),
goes on until the end (since no `end` is given), and uses a step size of `2`
(i.e., selects every second element).
::: {.callout-note}
## Exercise: Slice Bounds
What does the following program print?
```python
element = 'lithium'
print(element[0:20])
print(element[-1:3])
```
::: {.callout-tip collapse="true"}
## Solution
## Solution
```output
lithium
```
The first statement prints the whole string, since the slice goes beyond the total length of the string.
The second statement returns an empty string, because the slice goes "out of bounds" of the string.
::: {.callout-note}
## Exercise: Sort and Sorted
What do these two programs print?
In simple terms, explain the difference between `sorted(letters)` and `letters.sort()`.
```python
# Program A
letters = list('gold')
result = sorted(letters)
print('letters is', letters, 'and result is', result)
```
```python
# Program B
letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)
```
::: {.callout-tip collapse="true"}
## Solution
## Solution
Program A prints
```output
letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']
```
Program B prints
```output
letters is ['d', 'g', 'l', 'o'] and result is None
```
`sorted(letters)` returns a sorted copy of the list `letters` (the original
list `letters` remains unchanged), while `letters.sort()` sorts the list
`letters` in-place and does not return anything.
::: {.callout-note}
## Exercise: Copying (or Not)
What do these two programs print?
In simple terms, explain the difference between `new = old` and `new = old[:]`.
```python
# Program A
old = list('gold')
new = old # simple assignment
new[0] = 'D'
print('new is', new, 'and old is', old)
```
```python
# Program B
old = list('gold')
new = old[:] # assigning a slice
new[0] = 'D'
print('new is', new, 'and old is', old)
```
::: {.callout-tip collapse="true"}
## Solution
## Solution
Program A prints
```output
new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
```
Program B prints
```output
new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
```
`new = old` makes `new` a reference to the list `old`; `new` and `old` point
towards the same object.
`new = old[:]` however creates a new list object `new` containing all elements
from the list `old`; `new` and `old` are different objects.
::: {.callout-important}
## Key Points
- A list stores many values in a single structure.
- Use an item's index to fetch it from a list.
- Lists' values can be replaced by assigning to them.
- Appending items to a list lengthens it.
- Use `del` to remove items from a list entirely.
- The empty list contains no values.
- Lists may contain values of different types.
- Character strings can be indexed like lists.
- Character strings are immutable.
- Indexing beyond the end of the collection is an error.