Skip to content

Commit 292289b

Browse files
committed
Added some notes on using * on the left-hand side for unpacking.
1 parent da351ae commit 292289b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,20 @@ This will pack all the values into a `list`/`tuple`.
222222
>>> combined_fruits
223223
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
224224

225-
# If the * operator is used on the left side of "=" the result is a list
225+
# If the * operator is used on the left side of "=" the result is a list.
226+
# Note the trailing comma.
226227
>>> *combined_fruits_too, = *fruits, *more_fruits
227228
>>> combined_fruits_too
228229
['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
230+
231+
# A list literal can be used instead, but might not be as readable.
232+
>>> [*combined_fruits_too] = *fruits, *more_fruits
233+
>>> combined_fruits_too
234+
['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
229235
```
230236

237+
For more details on the use of `*` and `**`, check out [PEP 3132][pep-3132] and [PEP 448][pep-448].
238+
231239
### Packing a dictionary with `**`
232240

233241
Packing a dictionary is done by using the `**` operator.
@@ -370,6 +378,8 @@ Since `zip()` takes multiple iterables and returns a `list` of `tuples` with the
370378
[items]: https://docs.python.org/3/library/stdtypes.html#dict.items
371379
[multiple assignment]: https://www.geeksforgeeks.org/assigning-multiple-variables-in-one-line-in-python/
372380
[packing and unpacking]: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/
381+
[pep-448]: https://peps.python.org/pep-0448/
382+
[pep-3132]: https://peps.python.org/pep-3132/
373383
[sorting algorithms]: https://realpython.com/sorting-algorithms-python/
374384
[unpacking]: https://www.geeksforgeeks.org/unpacking-arguments-in-python/?ref=rp
375385
[view-objects]: https://docs.python.org/3/library/stdtypes.html#dict-views

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,16 @@ This will pack all the values into a `list`/`tuple`.
213213
>>> combined_fruits
214214
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
215215

216-
# If the * operator is used on the left side of "=" the result is a list
216+
# If the * operator is used on the left side of "=" the result is a list.
217+
# Note the trailing comma.
217218
>>> *combined_fruits_too, = *fruits, *more_fruits
218219
>>> combined_fruits_too
219220
['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
220221
```
221222

223+
For more background on using `*` on the left-hand side, see [PEP 3132][pep-3132].
224+
225+
222226
### Packing a dictionary with `**`
223227

224228
Packing a dictionary is done by using the `**` operator.
@@ -361,6 +365,7 @@ Since `zip()` takes multiple iterables and returns a `list` of `tuples` with the
361365
[items]: https://docs.python.org/3/library/stdtypes.html#dict.items
362366
[multiple assignment]: https://www.geeksforgeeks.org/assigning-multiple-variables-in-one-line-in-python/
363367
[packing and unpacking]: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/
368+
[pep-3132]: https://peps.python.org/pep-3132/
364369
[sorting algorithms]: https://realpython.com/sorting-algorithms-python/
365370
[unpacking]: https://www.geeksforgeeks.org/unpacking-arguments-in-python/?ref=rp
366371
[view-objects]: https://docs.python.org/3/library/stdtypes.html#dict-views

0 commit comments

Comments
 (0)