Skip to content

Commit

Permalink
differences for PR #679
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Apr 19, 2024
1 parent 26dd1a5 commit d1cbc0d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
50 changes: 39 additions & 11 deletions 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
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"episodes/08-data-frames.md" "af0057242e5f63f0c049f58ad66f1cbb" "site/built/08-data-frames.md" "2023-08-29"
"episodes/09-plotting.md" "d701a7c8d39329d1786b48b32063ffc8" "site/built/09-plotting.md" "2024-03-17"
"episodes/10-lunch.md" "0624bfa89e628df443070e8c44271b33" "site/built/10-lunch.md" "2023-05-02"
"episodes/11-lists.md" "1257daeb542377a3b04c6bec0d0ffee1" "site/built/11-lists.md" "2023-07-24"
"episodes/11-lists.md" "8335f624f3f47f46b7eb042c04d35a60" "site/built/11-lists.md" "2024-04-19"
"episodes/12-for-loops.md" "1da6e4e57a25f8d4fd64802c2eb682c4" "site/built/12-for-loops.md" "2023-05-02"
"episodes/13-conditionals.md" "2739086f688f386c32ce56400c6b27e2" "site/built/13-conditionals.md" "2024-02-16"
"episodes/14-looping-data-sets.md" "fb2992c34b244b375302ffb15bd25b8d" "site/built/14-looping-data-sets.md" "2024-03-05"
Expand Down

0 comments on commit d1cbc0d

Please sign in to comment.