Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions episodes/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ exercises: 10
::::::::::::::::::::::::::::::::::::::::::::::::::

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

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

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.

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

### Character strings are immutable.

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.
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.

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

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

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.
Lists are **objects**, which means they provide methods that allow us to perform common operations. A **method** is called using the syntax `object.method()`.

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.


### Appending items to a list.

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.

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

## Use `del` to remove items from a list entirely.
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.

```python
numbers = [1, 2, 3, 4, 6]
print(f'numbers before: {numbers}')
numbers.insert(4, 5)
print(f'numbers after: {numbers}')
```

```output
numbers before: [1, 2, 3, 4, 6]
numbers after: [1, 2, 3, 4, 5, 6]
```

### Removing items from a list.

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.

`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 = ...`) .

```python
primes = [2, 3, 5, 7, 11]
print(f'primes before: {primes}')
del primes[4]
print(f'primes after: {primes}')
drinks = ['water', 'tea', 'coffee', 'milk']
print(f'drinks before: {drinks}')
drinks.pop()
print(f'drinks after first pop: {drinks}')
val = drinks.pop(0)
print(f'drinks after second pop: {drinks}')
print(val)
```

```output
primes before: [2, 3, 5, 7, 11]
primes after: [2, 3, 5, 7]
drinks before: ['water', 'tea', 'coffee', 'milk']
drinks after first pop: ['water', 'tea', 'coffee']
drinks after second pop: ['tea', 'coffee']
water
```

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.

```python
animals = ['dog', 'bird', 'shark', 'dog']
print(f'animals before: {animals}')
animals.remove('dog')
print(f'animals after: {animals}')
```

```output
animals before: ['dog', 'bird', 'shark', 'dog']
animals after: ['bird', 'shark', 'dog']
```

::::::::::::::::::::::::::::::::::::::: challenge

Expand Down Expand Up @@ -223,8 +263,8 @@ print(f'second time: {values}')
```

```output
first time [1, 3, 5]
second time [3, 5]
first time: [1, 3, 5]
second time: [3, 5]
```

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

```python
resources = ['books','DVDs','maps','databases']
resources = ['books', 'DVDs', 'maps', 'databases']
print(resources[-1])
```

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

::::::::::::::: solution

Expand All @@ -256,7 +297,7 @@ databases

1. A negative index begins at the final element.
2. It removes the final element of the list.

3. It will return the final element of the list.

:::::::::::::::::::::::::

Expand All @@ -268,8 +309,7 @@ databases
- 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.
- Use list methods to help add and remove items
- Lists may contain values of different types.
- Character strings can be indexed like lists.
- Character strings are immutable.
Expand Down