Skip to content

Commit

Permalink
Added some notes on using * on the left-hand side for unpacking.
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyG committed Nov 18, 2024
1 parent da351ae commit 292289b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion concepts/unpacking-and-multiple-assignment/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,20 @@ This will pack all the values into a `list`/`tuple`.
>>> combined_fruits
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")

# If the * operator is used on the left side of "=" the result is a list
# If the * operator is used on the left side of "=" the result is a list.
# Note the trailing comma.
>>> *combined_fruits_too, = *fruits, *more_fruits
>>> combined_fruits_too
['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']

# A list literal can be used instead, but might not be as readable.
>>> [*combined_fruits_too] = *fruits, *more_fruits
>>> combined_fruits_too
['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
```

For more details on the use of `*` and `**`, check out [PEP 3132][pep-3132] and [PEP 448][pep-448].

### Packing a dictionary with `**`

Packing a dictionary is done by using the `**` operator.
Expand Down Expand Up @@ -370,6 +378,8 @@ Since `zip()` takes multiple iterables and returns a `list` of `tuples` with the
[items]: https://docs.python.org/3/library/stdtypes.html#dict.items
[multiple assignment]: https://www.geeksforgeeks.org/assigning-multiple-variables-in-one-line-in-python/
[packing and unpacking]: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/
[pep-448]: https://peps.python.org/pep-0448/
[pep-3132]: https://peps.python.org/pep-3132/
[sorting algorithms]: https://realpython.com/sorting-algorithms-python/
[unpacking]: https://www.geeksforgeeks.org/unpacking-arguments-in-python/?ref=rp
[view-objects]: https://docs.python.org/3/library/stdtypes.html#dict-views
7 changes: 6 additions & 1 deletion exercises/concept/locomotive-engineer/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,16 @@ This will pack all the values into a `list`/`tuple`.
>>> combined_fruits
("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")

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

For more background on using `*` on the left-hand side, see [PEP 3132][pep-3132].


### Packing a dictionary with `**`

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

0 comments on commit 292289b

Please sign in to comment.