Skip to content

Commit 3dcbf4c

Browse files
authored
Fixed dict.items refrences and text. (#3782)
1 parent b13c61e commit 3dcbf4c

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

concepts/unpacking-and-multiple-assignment/about.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ ValueError: too many values to unpack (expected 1)
129129

130130
### Unpacking a list/tuple with `*`
131131

132-
When [unpacking a `list`/`tuple`][packing and unpacking] you can use the `*` operator to capture the "leftover" values.
132+
When [unpacking a `list`/`tuple`][packing and unpacking] you can use the `*` operator to capture "leftover" values.
133133
This is clearer than slicing the `list`/`tuple` (_which in some situations is less readable_).
134-
For example, we can extract the first element and then assign the remaining values into a new `list` without the first element:
134+
For example, we can extract the first element and pack the remaining values into a new `list` without the first element:
135135

136136
```python
137137
>>> fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
@@ -169,7 +169,7 @@ We can also use `*` in deep unpacking:
169169

170170
### Unpacking a dictionary
171171

172-
[Unpacking a dictionary][packing and unpacking] is a bit different than unpacking a `list`/`tuple`.
172+
[Unpacking a dictionary][packing and unpacking] is a bit different from unpacking a `list`/`tuple`.
173173
Iteration over dictionaries defaults to the **keys**.
174174
So when unpacking a `dict`, you can only unpack the **keys** and not the **values**:
175175

@@ -180,7 +180,7 @@ So when unpacking a `dict`, you can only unpack the **keys** and not the **value
180180
"apple"
181181
```
182182

183-
If you want to unpack the values then you can use the `values()` method:
183+
If you want to unpack the values then you can use the `<dict>.values()` method:
184184

185185
```python
186186
>>> fruits_inventory = {"apple": 6, "banana": 2, "cherry": 3}
@@ -189,9 +189,9 @@ If you want to unpack the values then you can use the `values()` method:
189189
6
190190
```
191191

192-
If both **keys** and **values** are needed, use the `items()` method.
193-
Using `items()` will generate tuples with **key-value** pairs.
194-
This is because of [`dict.items()` generates an iterable with key-value `tuples`][items].
192+
If both **keys** and **values** are needed, use the [`<dict>.items()`][items] method.
193+
`<dict>.items()` generates an [iterable view][view-objects] containing **key-value** pairs.
194+
These can be unpacked into a `tuple`:
195195

196196
```python
197197
>>> fruits_inventory = {"apple": 6, "banana": 2, "cherry": 3}
@@ -367,8 +367,9 @@ Since `zip()` takes multiple iterables and returns a `list` of `tuples` with the
367367
```
368368

369369
[args and kwargs]: https://www.geeksforgeeks.org/args-kwargs-python/
370-
[items]: https://www.geeksforgeeks.org/python-dictionary-items-method/
370+
[items]: https://docs.python.org/3/library/stdtypes.html#dict.items
371371
[multiple assignment]: https://www.geeksforgeeks.org/assigning-multiple-variables-in-one-line-in-python/
372372
[packing and unpacking]: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/
373373
[sorting algorithms]: https://realpython.com/sorting-algorithms-python/
374374
[unpacking]: https://www.geeksforgeeks.org/unpacking-arguments-in-python/?ref=rp
375+
[view-objects]: https://docs.python.org/3/library/stdtypes.html#dict-views

exercises/concept/locomotive-engineer/.docs/introduction.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The special operators `*` and `**` are often used in unpacking contexts and with
99
`*<variable_name>` and `**<variable_name>` should not be confused with `*` and `**`. While `*` and `**` are used for multiplication and exponentiation respectively, `*<variable_name>` and `**<variable_name>` are used as packing and unpacking operators.
1010
~~~~
1111

12+
1213
## Multiple assignment
1314

1415
In multiple assignment, the number of variables on the left side of the assignment operator (`=`) must match the number of values on the right side.
@@ -55,6 +56,7 @@ For example:
5556

5657
Since `tuples` are immutable, you can't swap elements in a `tuple`.
5758

59+
5860
## Unpacking
5961

6062
~~~~exercism/note
@@ -80,9 +82,10 @@ If there are values that are not needed then you can use `_` to flag them:
8082
"cherry"
8183
```
8284

85+
8386
### Deep unpacking
8487

85-
Unpacking and assigning values from a `list`/`tuple` inside of a `list` or `tuple` (_also known as nested lists/tuples_), works in the same way a shallow unpacking does, but often needs qualifiers to clarify the values context or position:
88+
Unpacking and assigning values from a `list`/`tuple` enclosed inside a `list` or `tuple` (_also known as nested lists/tuples_) works in the same way a shallow unpacking doesbut often needs qualifiers to clarify the context or position:
8689

8790
```python
8891
>>> fruits_vegetables = [["apple", "banana"], ["carrot", "potato"]]
@@ -119,7 +122,7 @@ ValueError: too many values to unpack (expected 1)
119122

120123
When [unpacking a `list`/`tuple`][packing and unpacking] you can use the `*` operator to capture the "leftover" values.
121124
This is clearer than slicing the `list`/`tuple` (_which in some situations is less readable_).
122-
For example, we can extract the first element and then assign the remaining values into a new `list` without the first element:
125+
For example, the first element can be extracted and then the remaining values can be placed into a new `list` without the first element:
123126

124127
```python
125128
>>> fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
@@ -157,7 +160,7 @@ We can also use `*` in deep unpacking:
157160

158161
### Unpacking a dictionary
159162

160-
[Unpacking a dictionary][packing and unpacking] is a bit different than unpacking a `list`/`tuple`.
163+
[Unpacking a dictionary][packing and unpacking] is a bit different from unpacking a `list`/`tuple`.
161164
Iteration over dictionaries defaults to the **keys**.
162165
So when unpacking a `dict`, you can only unpack the **keys** and not the **values**:
163166

@@ -168,7 +171,7 @@ So when unpacking a `dict`, you can only unpack the **keys** and not the **value
168171
"apple"
169172
```
170173

171-
If you want to unpack the values then you can use the `values()` method:
174+
If you want to unpack the values then you can use the `<dict>.values()` method:
172175

173176
```python
174177
>>> fruits_inventory = {"apple": 6, "banana": 2, "cherry": 3}
@@ -177,9 +180,9 @@ If you want to unpack the values then you can use the `values()` method:
177180
6
178181
```
179182

180-
If both **keys** and **values** are needed, use the `items()` method.
181-
Using `items()` will generate tuples with **key-value** pairs.
182-
This is because of [`dict.items()` generates an iterable with key-value `tuples`][items].
183+
If both **keys** and **values** are needed, use the [`<dict>.items()`][items] method.
184+
`<dict>.items()` generates an [iterable view][view-objects] containing **key-value** pairs.
185+
These can be unpacked into a `tuple`:
183186

184187
```python
185188
>>> fruits_inventory = {"apple": 6, "banana": 2, "cherry": 3}
@@ -238,8 +241,8 @@ This will pack all **key**-**value** pairs from one dictionary into another dict
238241
### Packing with function parameters
239242

240243
When you create a function that accepts an arbitrary number of arguments, you can use [`*args` or `**kwargs`][args and kwargs] in the function definition.
241-
`*args` is used to pack an arbitrary number of positional (non-keyworded) arguments and
242-
`**kwargs` is used to pack an arbitrary number of keyword arguments.
244+
`*args` is used to pack an arbitrary number of positional (_non-keyword_) arguments as a `tuple` and
245+
`**kwargs` is used to pack an arbitrary number of keyword arguments as a dictionary.
243246

244247
Usage of `*args`:
245248

@@ -355,8 +358,9 @@ Since `zip()` takes multiple iterables and returns a `list` of `tuples` with the
355358
```
356359

357360
[args and kwargs]: https://www.geeksforgeeks.org/args-kwargs-python/
358-
[items]: https://www.geeksforgeeks.org/python-dictionary-items-method/
361+
[items]: https://docs.python.org/3/library/stdtypes.html#dict.items
359362
[multiple assignment]: https://www.geeksforgeeks.org/assigning-multiple-variables-in-one-line-in-python/
360363
[packing and unpacking]: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/
361364
[sorting algorithms]: https://realpython.com/sorting-algorithms-python/
362365
[unpacking]: https://www.geeksforgeeks.org/unpacking-arguments-in-python/?ref=rp
366+
[view-objects]: https://docs.python.org/3/library/stdtypes.html#dict-views

0 commit comments

Comments
 (0)