Syntax
a, b, *rest = iterableExamples
Basic Unpacking
Assigning each item of an iterable to its own variable.
coordinates = (10, 20, 30)
x, y, z = coordinates
print(x, y, z) # 10 20 30
first, second = ["Fola", "Zain"]
print(first, second)Star Unpacking
Using * to capture multiple leftover items into a list.
numbers = [1, 2, 3, 4, 5]
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
head, *tail = numbers
print(head, tail) # 1 [2, 3, 4, 5]Unpacking in Function Calls
Using * and ** to spread a list or dictionary into a function's arguments.
def add(a, b, c):
return a + b + c
numbers = [1, 2, 3]
print(add(*numbers)) # 6, same as add(1, 2, 3)
def greet(name, greeting):
print(f"{greeting}, {name}!")
info = {"name": "Fola", "greeting": "Hi"}
greet(**info) # unpacks dict into keyword argumentsUnpacking Nested Structures
Unpacking works with nested tuples and lists too.
point = (("x", 3), ("y", 4))
(x_label, x_val), (y_label, y_val) = point
print(f"{x_label}={x_val}, {y_label}={y_val}") # x=3, y=4
# Common pattern: unpacking while iterating
pairs = [(1, "one"), (2, "two"), (3, "three")]
for number, word in pairs:
print(f"{number}: {word}")Best practices
- Use unpacking instead of manual indexing when you know exactly how many items an iterable contains
- Use *rest to flexibly capture 'everything else' without knowing the exact length in advance
- Use *args-style unpacking (*numbers) to spread a list into a function call rather than passing the list itself when separate arguments are expected
- Unpacking raises a ValueError if the number of variables does not match the number of items - use * to handle variable-length iterables safely
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
Lists
A list is Python's built-in ordered, mutable collection type, created with square brackets. Lists can hold items of any type - even a mix of types - and support indexing, slicing, and a wide range of built-in methods for adding, removing, and reordering elements. Because they are mutable, lists can be changed in place after creation, which makes them the go-to structure for collections that grow or shrink over time.Tuples
A tuple is an ordered, immutable collection, created with parentheses (or often just commas). Once created, a tuple's contents cannot be changed, added to, or removed - this immutability makes tuples faster than lists and safe to use as dictionary keys or in sets. Tuples are commonly used for fixed collections of related values, like coordinates or RGB colors, and for returning multiple values from a function.Dictionaries
A dictionary stores data as key-value pairs, created with curly braces. Keys must be unique and hashable (strings, numbers, or tuples are common choices), while values can be anything, including other dictionaries or lists. Since Python 3.7, dictionaries maintain insertion order. They are one of the most heavily used data structures in Python, ideal for representing structured records, lookups, and mappings.Sets
A set is an unordered collection of unique, hashable items, created with curly braces or the set() function. Sets automatically eliminate duplicates and support fast membership testing, along with mathematical set operations like union, intersection, and difference. They're especially useful for deduplicating data and for comparing two collections to find overlaps or differences.
A list is Python's built-in ordered, mutable collection type, created with square brackets. Lists can hold items of any type - even a mix of types - and support indexing, slicing, and a wide range of built-in methods for adding, removing, and reordering elements. Because they are mutable, lists can be changed in place after creation, which makes them the go-to structure for collections that grow or shrink over time.Tuples
A tuple is an ordered, immutable collection, created with parentheses (or often just commas). Once created, a tuple's contents cannot be changed, added to, or removed - this immutability makes tuples faster than lists and safe to use as dictionary keys or in sets. Tuples are commonly used for fixed collections of related values, like coordinates or RGB colors, and for returning multiple values from a function.Dictionaries
A dictionary stores data as key-value pairs, created with curly braces. Keys must be unique and hashable (strings, numbers, or tuples are common choices), while values can be anything, including other dictionaries or lists. Since Python 3.7, dictionaries maintain insertion order. They are one of the most heavily used data structures in Python, ideal for representing structured records, lookups, and mappings.Sets
A set is an unordered collection of unique, hashable items, created with curly braces or the set() function. Sets automatically eliminate duplicates and support fast membership testing, along with mathematical set operations like union, intersection, and difference. They're especially useful for deduplicating data and for comparing two collections to find overlaps or differences.