unpacking
In Python, unpacking lets you assign or pass multiple values at once by expanding an iterable into individual items. You’ll see it in assignments for parallel name binding and in expressions and function calls via the iterable unpacking (*) and dictionary unpacking (**) operators.
In assignments, the right-hand side must be an iterable, and the left-hand side is a comma-separated sequence of assignment targets (often written as a tuple of names). The number of values and targets must match. Alternatively, you can use a single starred target (*rest) to collect multiple values into a list.
In function calls, *iterable expands an iterable into positional arguments, while **dictionary expands a dictionary into keyword arguments.
Example
Parallel assignment and value swapping:
>>> a, b = 1, 2
>>> a, b
(1, 2)
>>> a, b = b, a
>>> a, b
(2, 1)
Starred targets:
>>> first, *middle, last = [10, 20, 30, 40]
>>> first
10
>>> middle
[20, 30]
>>> last
40
Function calls:
>>> def greet(greeting, name):
... return f"{greeting}, {name}!"
...
>>> args = ("Hello", "Pythonista")
>>> greet(*args)
'Hello, Pythonista!'
>>> def connect(host, port, timeout=10):
... return host, port, timeout
...
>>> conn_params = {"host": "localhost", "port": 8000}
>>> connect(**conn_params)
('localhost', 8000, 10)
Unpacking in expressions:
>>> numbers = [0, *range(1, 4), 4]
>>> numbers
[0, 1, 2, 3, 4]
Here, the result of range(1, 4) is unpacked into the list.
Related Resources
Tutorial
Python's list Data Type: A Deep Dive With Examples
In this tutorial, you'll dive deep into Python's lists. You'll learn how to create them, update their content, populate and grow them, and more. Along the way, you'll code practical examples that will help you strengthen your skills with this fundamental data type in Python.
For additional information on related topics, take a look at the following resources:
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Dictionaries in Python (Tutorial)
- Defining Your Own Python Function (Tutorial)
- Exploring Python's list Data Type With Examples (Course)
- Exploring Python's tuple Data Type With Examples (Course)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Using Dictionaries in Python (Course)
- Dictionaries in Python (Quiz)
- Defining and Calling Python Functions (Course)
- Defining Your Own Python Function (Quiz)
- Defining and Calling Python Functions (Quiz)
By Leodanis Pozo Ramos • Updated Jan. 9, 2026