Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Episode 11 "Lists": List combinations, nested lists, removing list elements #679

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 39 additions & 11 deletions episodes/11-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,38 @@ primes has become: [2, 3, 5, 7]
- 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:

## Multiple lists can be combined.
- `extend` is a list *method* like `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]
primes = [2, 3, 5, 7]
more_primes = [11, 13, 17, 19]
print('primes is currently:', primes)
primes.extend(teen_primes)
primes.extend(more_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.
- We can also combine lists using the `+` operator, but instead of altering an existing list, it creates a new one.

```python
primes = [2, 3, 5, 7]
more_primes = [11, 13, 17, 19]
combined_primes = primes + more_primes
print('combined_primes is now:', combined_primes)
```

```output
combined_primes is now: [2, 3, 5, 7, 11, 13, 17, 19]
```


## Use `del` to remove items from a list entirely.
## Items can be removed from a list.

- 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.
Expand All @@ -125,6 +134,20 @@ primes before removing last item: [2, 3, 5, 7, 9]
primes after removing last item: [2, 3, 5, 7]
```

- The list method `pop` is helpful, when you want to fetch an item **AND** remove it from the list.
- Similarly to `del`, we use it by providing the desired index: `list.pop(index)`.
```python
primes = [2, 3, 5, 7, 9]
wrong_prime = primes.pop(4)
print('primes after removing last item:', primes)
print('The wrong prime was: ', wrong_prime)
```

```output
primes after removing last item: [2, 3, 5, 7]
The wrong prime was: 9
```

## The empty list contains no values.

- Use `[]` on its own to represent a list that doesn't contain any values.
Expand All @@ -140,6 +163,11 @@ primes after removing last item: [2, 3, 5, 7]
goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
```

- This implies that lists can store other lists, allowing arbitrary nested lists.
```python
more_goals = [4, 'Explore lists.', [5, 'Understand lists within lists.', [6, 'Master Python lists.']]]
```

## Character strings can be indexed like lists.

- Get single characters from a character string using indexes in square brackets.
Expand Down
Loading