Collections
Unpacking
Unpacking binds multiple names from one iterable or mapping. It makes the structure of data visible at the point where values are introduced.
Source
point = (3, 4)
x, y = point
print(x, y)Output
3 4Starred unpacking handles variable-length sequences by collecting the middle or remaining values. This keeps common head-tail patterns readable.
Source
first, *middle, last = [1, 2, 3, 4]
print(first, middle, last)Output
1 [2, 3] 4Dictionary unpacking with ** connects structured data to function calls. It is widely used in configuration, adapters, and code that bridges APIs.
Dictionary unpacking with ** connects structured data to function calls. It is widely used in configuration, adapters, and code that bridges APIs.
Source
def describe(name, language):
print(name, language)
data = {"name": "Ada", "language": "Python"}
describe(**data)Output
Ada PythonNotes
- Starred unpacking collects the remaining values into a list.
- Dictionary unpacking with ** is common when calling functions with structured data.
- Prefer indexing when you need one position; prefer unpacking when naming several positions makes the shape clearer.
See also
- related: Tuples
- next depth: Multiple Return Values
- next depth: Args and Kwargs
- related: Dictionaries
Run the complete example
Expected output
3 4
1 [2, 3] 4
Ada Python
Execution time appears here after you run the example.