In Python, packing and unpacking are concepts related to handling multiple values with tuples, lists, and dictionaries.
Packing: This refers to the process of taking multiple values and putting them into a single variable. For example, when you assign multiple values to a single variable using a tuple
, list
or dict
, you are packing those values.
packed_tuple = 1, 2, 3
another_packed_tuple = (1, 2, 3)
packed_list = [4, 5, 6]
packed_dict = {"a": 1, "b": 2}
Unpacking: This is the reverse process, where you take a collection of values and extract them into individual variables. This is useful when you want to work with each value separately.
packed_tuple = 1, 2, 3
a, b, c = packed_tuple
assert a == 1 and b == 2 and c == 3
packed_list = [4, 5, 6]
x, y, z = packed_list
assert x == 4 and y == 5 and z == 6
packed_dict = {"a": 1, "b": 2}
a, b = packed_dict
assert a == "a" and b == "b"
Note that when unpacking a dictionary, you are actually unpacking its keys, not the values. This is similar to how dictionaries behave in a for
loop by default, where iteration is over the keys.
In unpacking, the number of variables on the left must match the number of elements in the collection being unpacked. Python also supports Extended Unpacking, which allows you to capture remaining elements using the *
operator.