Skip to content

Commit e8e1d41

Browse files
committed
Further edited language in list methods about.md and introduction.md.
1 parent 7cb2b57 commit e8e1d41

2 files changed

Lines changed: 54 additions & 29 deletions

File tree

concepts/list-methods/about.md

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# About
22

3-
A [`list`][list] is a mutable collection of items in _sequence_. Like most collections (_see the built-ins [`tuple`][tuple], [`dict`][dict] and [`set`][set]_), lists can hold references to any (or multiple) data type(s) - including other lists. They can be copied in whole or in part via [slice notation][slice notation]. Like any [sequence][sequence type], elements within `lists` are referenced by `0-based index` number.
3+
A [`list`][list] is a mutable collection of items in _sequence_. Like most collections (_see the built-ins [`tuple`][tuple], [`dict`][dict] and [`set`][set]_), lists can hold references to any (or multiple) data type(s) - including other lists.
4+
They're considered a [sequence][sequence type] in Python, and can be copied in whole or in part via [slice notation][slice notation].
5+
Like any sequence, elements within `lists` can be referenced by `0-based index` number from the left, or `-1-based index` number from the right.
46

5-
Lists support both [common][common sequence operations] and [mutable][mutable sequence operations] sequence opterations like `min(<list>)`/`max(<list>)`, `<list>.index()`, `<list>.append()` and `<list>.reverse()`. Items can be iterated over using the `for item in <list>` construct, and `for item in enumerate(<list>)` can be used when both the element and index are needed.
7+
Lists support both [common][common sequence operations] and [mutable][mutable sequence operations] sequence opterations like `min(<list>)`/`max(<list>)`, `<list>.index()`, `<list>.append()` and `<list>.reverse()`.
8+
Items can be iterated over using the `for item in <list>` construct, and `for item in enumerate(<list>)` can be used when both the element and element index are needed.
69

7-
Python provides many useful [methods][list-methods] for working with lists. Let's take a look at some of them.
10+
Python provides many useful [methods][list-methods] for working with lists.
811

9-
Keep in mind that when you manipulate a list with a list-method, **you alter the list** object that has been passed into the method. If you do not wish to mutate your original `list`, you will need to at least make a `shallow copy` of it via slice or `<list>.copy()`.
12+
Becasue `lists` are mutable, list-methods **alter the original list object** passed into the method.
13+
If mutation is undesirable, the original `list` needs to at least be copied via `shallow copy` via `slice` or `<list>.copy()`.
1014

1115
## Adding Items
1216

13-
If you want to add an item to the end of an existing list, use `list.append()`:
17+
Adding items to the end of an existing list can be done via `<list>.append(<item>)`:
1418

1519
```python
1620
>>> numbers = [1, 2, 3]
@@ -19,26 +23,26 @@ If you want to add an item to the end of an existing list, use `list.append()`:
1923
[1, 2, 3, 9]
2024
```
2125

22-
Rather than _appending_, `list.insert()` gives you the ability to add the item to a _specific index_ in the list.
26+
Rather than _appending_, `<list>.insert(<index>, <item>)` adds the item to a _specific index_ within the list.
27+
`<index>` is the index of the item _before which_ you want the new item to appear.
28+
`<item>` is the element to be inserted.
2329

24-
`list.insert()` takes 2 parameters:
25-
26-
1. the index of the item _before which_ you want the new item to appear
27-
2. the item to be inserted
28-
29-
**Note**: If the given index is 0, the item will be added to the start of the list. If the supplied index is greater than the last index on the list, the item will be added in the final position -- the equivalent of using `list.append()`.
30+
**Note**: If `<index>` is 0, the item will be added to the start of the list.
31+
If `<index>` is greater than the final index on the list, the item will be added in the final position -- the equivalent of using `<list>.append(<item>)`.
3032

3133
```python
3234
>>> numbers = [1, 2, 3]
3335
>>> numbers.insert(0, -2)
3436
>>> numbers
3537
[-2, 1, 2, 3]
38+
3639
>>> numbers.insert(1, 0)
3740
>>> numbers
3841
[-2, 0, 1, 2, 3]
3942
```
4043

41-
If you have an iterable that you would like to _combine_ with your current list (concatenating the two), `list.extend()` can be used. `list.extend()` will _unpack_ the supplied iterable, adding its elements in the same order to the end of your list (_using `.append()` in this circumstance would add the entire iterable as a **single item**._).
44+
An `iterable` can be _combined_ with an existing list (concatenating the two) via `<list>.extend(<iterable>)`.
45+
`<list>.extend(<iterable>)` will _unpack_ the supplied iterable, adding its elements in the same order to the end of the target list (_using `<list>.append(<item>)` in this circumstance would add the entire iterable as a **single item**._).
4246

4347
```python
4448
>>> numbers = [1, 2, 3]
@@ -57,22 +61,25 @@ If you have an iterable that you would like to _combine_ with your current list
5761

5862
## Removing Items
5963

60-
To delete an item from a list use `list.remove()`, passing the item to be removed as an argument. `list.remove()` will throw a `ValueError` if the item is not present in the list.
64+
`<list>.remove(<item>)` can be used to remove an element from the list.
65+
`<list>.remove(<item>)` will throw a `ValueError` if the element is not present in the list.
6166

6267
```python
6368
>>> numbers = [1, 2, 3]
6469
>>> numbers.remove(2)
6570
>>> numbers
6671
[1, 3]
6772

68-
# Trying to remove a value that is not in the list throws a ValueError
73+
# Trying to remove a value that is not in the list throws a ValueError.
6974
>>> numbers.remove(0)
7075
ValueError: list.remove(x): x not in list
7176
```
7277

73-
Alternatively, using the `list.pop()` method will both remove **and** `return` an element for use.
78+
Alternatively, using `<list>.pop(<item>)` method will both remove **and** `return` an element for use.
7479

75-
`list.pop()` takes one optional parameter: the index of the item you need to remove and receive. If the optional index argument is not specified, the last element of the list will be removed and returned to you. If you specify an index number higher than the length of the list, you will get an `IndexError`.
80+
`<list>.pop(<item>)` takes one optional parameter: the `index` of the element to remove and return.
81+
If the optional `<index>` argument is not specified, the last element of the list will be removed and returned.
82+
If `<index>` is a higher number than the final index of the list, an `IndexError` will be thrown.
7683

7784
```python
7885
>>> numbers = [1, 2, 3]
@@ -82,15 +89,18 @@ Alternatively, using the `list.pop()` method will both remove **and** `return` a
8289
[2, 3]
8390
>>> numbers.pop()
8491
3
92+
8593
>>> numbers
8694
[2]
95+
96+
# This will throw an error because there is only index 0.
8797
>>> numbers.pop(1)
8898
Traceback (most recent call last):
8999
File "<stdin>", line 1, in <module>
90100
IndexError: pop index out of range
91101
```
92102

93-
If you want to remove all items from a `list`, use `list.clear()`. It does not take any parameters.
103+
All items can be removed from a `list` via `<list>.clear()`. It does not take any parameters.
94104

95105
```python
96106
>>> numbers = [1, 2, 3]
@@ -103,7 +113,7 @@ If you want to remove all items from a `list`, use `list.clear()`. It does not t
103113

104114
## Reversing and reordering
105115

106-
You can reverse the order of list elements _***in place**_ with `<list>.reverse()`. This will mutate the original list.
116+
The order of list elements can be reversed _**in place**_ with `<list>.reverse()`. This will mutate the original list.
107117

108118
```python
109119
>>> numbers = [1, 2, 3]
@@ -112,7 +122,10 @@ You can reverse the order of list elements _***in place**_ with `<list>.reverse(
112122
[3, 2, 1]
113123
```
114124

115-
List elements can also be sorted _**in place**_ using `list.sort()`. Internally, Python uses [`Timsort`][timsort] to arrange elements. The default order is _ascending_. Take a look at the Python docs for some [additional tips and techniques for sorting][sorting how to] lists effectively.
125+
List elements can be sorted _**in place**_ using `<list>.sort()`.
126+
Internally, Python uses [`Timsort`][timsort] to arrange the elements.
127+
The default order is _ascending_.
128+
The Python docs have [additional tips and techniques for sorting][sorting how to] `lists` effectively.
116129

117130
```python
118131
>>> names = ["Tony", "Natasha", "Thor", "Bruce"]
@@ -123,7 +136,7 @@ List elements can also be sorted _**in place**_ using `list.sort()`. Internally
123136
["Bruce", "Natasha", "Thor", "Tony"]
124137
```
125138

126-
If you want to sort a list in _descending_ order, pass a `reverse=True` argument:
139+
To sort a list in _descending_ order, pass a `reverse=True` argument to the method:
127140

128141
```python
129142
>>> names = ["Tony", "Natasha", "Thor", "Bruce"]
@@ -132,13 +145,17 @@ If you want to sort a list in _descending_ order, pass a `reverse=True` argument
132145
["Tony", "Thor", "Natasha", "Bruce"]
133146
```
134147

135-
For cases where mutating the original list is undesirable, the built-in functions [`sorted()`][sorted] and [`reversed()`][reversed] can be used. `sorted(<list>)` will return a sorted _copy_, and takes the same parameters as `<list>.sort()`. `reversed(<list>)` returns an _iterator_ that yields the list's items in reverse order. We'll talk about iterators more in later exercises.
148+
For cases where mutating the original `list` is undesirable, the built-in functions [`sorted()`][sorted] and [`reversed()`][reversed] can be used.
149+
`sorted(<list>)` will return a sorted _copy_, and takes the same parameters as `<list>.sort()`.
150+
`reversed(<list>)` returns an _iterator_ that yields the list's items in reverse order.
151+
`Iterators` will be covered in detail in another exercise.
136152

137153
<br>
138154

139155
## Occurrences of an item in a list
140156

141-
You can find the number of occurrences of an element in a list with the help of `list.count()`. It takes the item you need to tally as its argument and returns the total number of times it appears on the list.
157+
Finding the number of occurrences of an element in a list can be done with the help of `<list>.count(<item>)`.
158+
It returns the total number of times `<item>` appears as an element in the list.
142159

143160
```python
144161
>>> items = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3]
@@ -150,9 +167,9 @@ You can find the number of occurrences of an element in a list with the help of
150167

151168
## Finding the index of items
152169

153-
`list.index()` will return the index number of the _first occurrence_ of an item passed in. If there are no occurrences, a `ValueError` is raised. If you don't need the exact position of an item and are only checking that it is present inside the list, the built-in `in` operator is more efficient.
154-
155-
Indexing is zero-based, meaning the position of the first item is `0`.
170+
`<list>.index(<item>)` will return the index number of the _first occurrence_ of an item passed in.
171+
If there are no occurrences, a `ValueError` is raised.
172+
Indexing is 0-based from the left, meaning the position of the first item is index `0`.
156173

157174
```python
158175
>>> items = [7, 4, 1, 0, 2, 5]
@@ -162,7 +179,7 @@ Indexing is zero-based, meaning the position of the first item is `0`.
162179
ValueError: 10 is not in list
163180
```
164181

165-
You can also provide `start` and `end` indices to search within a specific section of the list:
182+
Providing `start` and `end` indices will search within a specific section of the list:
166183

167184
```python
168185
>>> names = ["Tina", "Leo", "Thomas", "Tina", "Emily", "Justin"]
@@ -172,6 +189,14 @@ You can also provide `start` and `end` indices to search within a specific secti
172189
3
173190
```
174191

192+
If the exact position of an element is not needed, the built-in `in` operator is more efficient for verifying membership.
193+
194+
```python
195+
>>> names = ["Tina", "Leo", "Thomas", "Tina", "Emily", "Justin"]
196+
>>> "Thomas" in names
197+
True
198+
```
199+
175200
<br>
176201

177202
## Making Copies
@@ -180,7 +205,7 @@ Remember that variables in Python are labels that point to underlying objects an
180205

181206
Assigning a `list` object to a new variable _name_ does not copy the object or any of its referenced data. Any change made to the items in the `list` using the new variable name will also _impact the original_.
182207

183-
`<list>.copy()` will create a new `list` object, but **will not** create new objects for the referenced list _elements_. This is called a `shallow copy`. A `shallow copy` is usually enough when you want to add or remove items from one of the `list` objects without modifying the other. But if there is any chance that the _underlying_ elements of a `list` might be accidentally mutated (_thereby mutating all related shallow copies_), [`copy.deepcopy()`][deepcopy] in the `copy` module should be used to create a complete or "deep" copy of **all** references and objects.
208+
`<list>.copy()` will create a new `list` object, but **will not** create new objects for the referenced list _elements_. This is called a `shallow copy`. A `shallow copy` is usually enough when you want to add or remove items from one of the `list` objects without modifying the other. But if there is any chance that the _underlying_ elements of a `list` might be accidentally mutated (_thereby mutating all related shallow copies_), [`copy.deepcopy()`][deepcopy] in the `copy` module should be used to create a complete or "deep" copy of **all** references and objects.
184209

185210
For a detailed explanation of names, values, list, and nested list behavior, take a look at this excellent blog post from [Ned Batchelder][ned batchelder] -- [Names and values: making a game board][names and values].
186211

concepts/list-methods/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
A [list][list] is a mutable collection of items in _sequence_. They're an extremely flexible and useful data structure that many built-in methods and operations in Python produce as output. Lists can hold reference to any (or multiple) data type(s) - including other lists or data structures such as [tuples][tuples], [sets][sets], or [dicts][dicts]. Python provides many useful and convenient [list methods][list-methods] for working with lists.
3+
A [list][list] is a mutable collection of items in _sequence_. They're an extremely flexible and useful data structure that many built-in methods and operations in Python produce as output. Lists can hold reference to any (or multiple) data type(s) - including other `lists` or data structures such as [tuples][tuples], [sets][sets], or [dicts][dicts]. Python provides many useful and convenient [methods][list-methods] for working with lists.
44

55
[tuples]: https://github.com/exercism/python/tree/main/concepts/tuples
66
[dicts]: https://github.com/exercism/python/tree/main/concepts/dicts

0 commit comments

Comments
 (0)