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 args and kwargs: Demystified
In this step-by-step tutorial, you'll learn how to use args and kwargs in Python to add more flexibility to your functions. You'll also take a closer look at the single and double-asterisk unpacking operators, which you can use to unpack any iterable object in Python.
For additional information on related topics, take a look at the following resources:
- Python's Assignment Operator: Write Robust Assignments (Tutorial)
- Python args and kwargs: Demystified (Course)
- Python's list Data Type: A Deep Dive With Examples (Tutorial)
- 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)
- Python args and kwargs: Demystified (Quiz)
- Python's Assignment Operator: Write Robust Assignments (Quiz)
- Exploring Python's list Data Type With Examples (Course)
- Python's list Data Type: A Deep Dive With Examples (Quiz)
- Exploring Python's tuple Data Type With Examples (Course)
- Python's tuple Data Type: A Deep Dive With Examples (Quiz)
- 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 and Calling Python Functions (Quiz)
- Defining Your Own Python Function (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 6, 2026