Skip to content

Commit caf74a7

Browse files
committed
markdown source builds
Auto-generated via `{sandpaper}` Source : 47e2312 Branch : main Author : Cody Hennesy <codyhennesy@gmail.com> Time : 2026-08-21 18:26:38 +0000 Message : Merge pull request #173 from clarktr1/main Update lists.md
1 parent e21d68d commit caf74a7

2 files changed

Lines changed: 62 additions & 22 deletions

File tree

‎lists.md‎

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ exercises: 10
1818
::::::::::::::::::::::::::::::::::::::::::::::::::
1919

2020
## A list stores many values in a single structure.
21-
The most popular kind of data collection in Python is the list. Lists have two primary important characteristics:
21+
A list is one of the most commonly used data structures in Python. Lists have two important characteristics:
2222

23-
1. They are mutable, i.e., they can be changed after they are created.
24-
2. They are heterogeneous, i.e., they can store values of many different types.
23+
1. They are `mutable`, i.e, they can be changed after they are created.
24+
2. They are `heterogeneous`, i.e, they can store values of many different types.
2525

2626
To create a new list, you can just put some values in square brackets with commas in between. Let's create a short list of some library metadata standards.
2727

@@ -76,7 +76,7 @@ List is now: ['bibframe', 'frbr', 'mets', 'mods']
7676

7777
### Character strings are immutable.
7878

79-
Unlike lists, we cannot change the characters in a string using its index value. In other words strings are *immutable* (cannot be changed in-place after creation), while 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.
79+
Unlike lists, we cannot change the characters in a string using its index value. Strings and lists can both be indexed and sliced. However, strings are immutable (cannot change in place), while lists are mutable.
8080

8181
```python
8282
librarian = 'Langanathan' # misspelled SR Ranganathan's name
@@ -100,9 +100,16 @@ f'First item in sublist: {mixed_list[2][0]}'
100100
First item in sublist: 10
101101
```
102102

103-
## Appending items to a list lengthens it.
103+
## Using list methods to make adding/removing easier.
104104

105-
Use `list_name.append` to add items to the end of a list. In Python, we would call `.append()` a *method* of the list object. You can use the syntax of `object.method()` to call methods.
105+
Lists are **objects**, which means they provide methods that allow us to perform common operations. A **method** is called using the syntax `object.method()`.
106+
107+
Lists provide methods for modifying their contents. We call these methods using dot notation. There are many methods associated with a list, but we will cover only a few of them.
108+
109+
110+
### Appending items to a list.
111+
112+
Use `list_name.append()` to add items to the end of a list. In Python, we would call `.append()` a *method* of the list object.
106113

107114
```python
108115
print(f'list was:{metadata}')
@@ -115,22 +122,55 @@ list was: ['bibframe', 'frbr', 'mets', 'mods']
115122
list is now: ['bibframe', 'frbr', 'mets', 'mods', 'oai-pmh']
116123
```
117124

118-
## Use `del` to remove items from a list entirely.
125+
We can also call `list_name.insert(index, value)` to add an item at a given position in the list. The first argument specifies where to place the new item, and the second argument is the item to add.
126+
127+
```python
128+
numbers = [1, 2, 3, 4, 6]
129+
print(f'numbers before: {numbers}')
130+
numbers.insert(4, 5)
131+
print(f'numbers after: {numbers}')
132+
```
133+
134+
```output
135+
numbers before: [1, 2, 3, 4, 6]
136+
numbers after: [1, 2, 3, 4, 5, 6]
137+
```
138+
139+
### Removing items from a list.
140+
141+
Use `list_name.pop()` to remove and return the last item in a list. If we provide an index, `.pop()` removes and _returns_ the item at that position. It raises an IndexError if the list is empty or the index is out of range.
119142

120-
`del list_name[index]` removes an item from a list and shortens the list. Unlike `.append()`, `del` is not a method, but a "statement" in Python. In the example below, `del` performs an "in-place" operation on a list of prime numbers. This means that the `primes` variable will be reassigned when you use the `del` statement, without needing to use an assignment operator (e.g., `primes = ...`) .
121143

122144
```python
123-
primes = [2, 3, 5, 7, 11]
124-
print(f'primes before: {primes}')
125-
del primes[4]
126-
print(f'primes after: {primes}')
145+
drinks = ['water', 'tea', 'coffee', 'milk']
146+
print(f'drinks before: {drinks}')
147+
drinks.pop()
148+
print(f'drinks after first pop: {drinks}')
149+
val = drinks.pop(0)
150+
print(f'drinks after second pop: {drinks}')
151+
print(val)
127152
```
128153

129154
```output
130-
primes before: [2, 3, 5, 7, 11]
131-
primes after: [2, 3, 5, 7]
155+
drinks before: ['water', 'tea', 'coffee', 'milk']
156+
drinks after first pop: ['water', 'tea', 'coffee']
157+
drinks after second pop: ['tea', 'coffee']
158+
water
159+
```
160+
161+
We can also call `list_name.remove(value)`, where value is the item you want to remove. This will remove the **first** occurrence of the item. If there are multiple, then `.remove()` will need to be called several times.
162+
163+
```python
164+
animals = ['dog', 'bird', 'shark', 'dog']
165+
print(f'animals before: {animals}')
166+
animals.remove('dog')
167+
print(f'animals after: {animals}')
132168
```
133169

170+
```output
171+
animals before: ['dog', 'bird', 'shark', 'dog']
172+
animals after: ['bird', 'shark', 'dog']
173+
```
134174

135175
::::::::::::::::::::::::::::::::::::::: challenge
136176

@@ -223,8 +263,8 @@ print(f'second time: {values}')
223263
```
224264

225265
```output
226-
first time [1, 3, 5]
227-
second time [3, 5]
266+
first time: [1, 3, 5]
267+
second time: [3, 5]
228268
```
229269

230270
:::::::::::::::::::::::::
@@ -239,12 +279,13 @@ second time [3, 5]
239279
Run the following code on your own and answer the following questions.
240280

241281
```python
242-
resources = ['books','DVDs','maps','databases']
282+
resources = ['books', 'DVDs', 'maps', 'databases']
243283
print(resources[-1])
244284
```
245285

246286
1. How does Python interpret a negative index value?
247-
2. If `resources` is a list, what does `del resources[-1]` do?
287+
2. If `resources` is a list, what does `resources.pop(-1)` do?
288+
3. What value does `resources.pop(-1)` return?
248289

249290
::::::::::::::: solution
250291

@@ -256,7 +297,7 @@ databases
256297

257298
1. A negative index begins at the final element.
258299
2. It removes the final element of the list.
259-
300+
3. It will return the final element of the list.
260301

261302
:::::::::::::::::::::::::
262303

@@ -268,8 +309,7 @@ databases
268309
- A list stores many values in a single structure.
269310
- Use an item's index to fetch it from a list.
270311
- Lists' values can be replaced by assigning to them.
271-
- Appending items to a list lengthens it.
272-
- Use `del` to remove items from a list entirely.
312+
- Use list methods to help add and remove items
273313
- Lists may contain values of different types.
274314
- Character strings can be indexed like lists.
275315
- Character strings are immutable.

‎md5sum.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"index.md" "caae338b4fd531ec1006192920874f97" "site/built/index.md" "2025-12-18"
66
"episodes/getting-started.md" "839114535f2cb89b08629d66a40a8589" "site/built/getting-started.md" "2025-12-18"
77
"episodes/variables.md" "b4410937474e0325bdd6b68a39d0b1b5" "site/built/variables.md" "2025-12-18"
8-
"episodes/lists.md" "1fc969bd1eb08110d754725d68bb8602" "site/built/lists.md" "2025-12-18"
8+
"episodes/lists.md" "83fc472c42f68dd20a1298c9454f799e" "site/built/lists.md" "2026-08-21"
99
"episodes/functions.md" "499deb6ac29cdd4a00134176ef890616" "site/built/functions.md" "2025-12-18"
1010
"episodes/libraries.md" "e3b3c8d3d492322fdf7a05006084c3f6" "site/built/libraries.md" "2025-12-18"
1111
"episodes/for-loops.md" "a44b90e5f6f451c1659b7f877e0c0c4d" "site/built/for-loops.md" "2025-12-18"

0 commit comments

Comments
 (0)