Packing and Unpacking in Python
Packing means collecting several values into one collection; unpacking means the reverse — spreading a collection's items back out into separate variables or arguments. Python does this constantly, often invisibly: multiple assignment, *args, and **kwargs are all forms of the same underlying idea.
Packing
a, b, c = 1, 2, 3 assigns three separate values, packing the right-hand side into an implicit tuple first.
Unpacking a sequence
x, y = point spreads a two-item collection directly into two variables.
A starred variable captures the rest
first, *rest = [1, 2, 3, 4] gives first the value 1 and rest the list [2, 3, 4].
Unpacking into a function call
func(*my_list) spreads a list’s items out as separate positional arguments.
Unpacking a dictionary into keyword arguments
func(**my_dict) passes each key-value pair as its own keyword argument.
first, *middle, last = [1, 2, 3, 4, 5]print(first, middle, last) coords = (3, 4) def show(x, y): print(f"x={x}, y={y}") show(*coords)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.