Packing and Unpacking Arguments in Python
Last Updated :
23 Jul, 2025
Python provides the concept of packing and unpacking arguments, which allows us to handle variable-length arguments efficiently. This feature is useful when we don’t know beforehand how many arguments will be passed to a function.
Packing Arguments
Packing allows multiple values to be combined into a single parameter using * (for tuples/lists) and ** (for dictionaries).
- *args (Non-keyword arguments): Packs multiple positional arguments into a tuple.
- **kwargs (Keyword arguments): Packs multiple keyword arguments into a dictionary.
1. Packing with *args
The *
operator allows us to pass multiple arguments to a function and pack them into a tuple.
Example Code:
Python
def sample(*args):
print("Packed arguments:", args)
sample(1, 2, 3, 4, "geeks for geeks")
OutputPacked arguments: (1, 2, 3, 4, 'geeks for geeks')
Explanation:
- The function takes any number of arguments.
- The
*args
packs all arguments into a tuple.
2. Packing with **kwargs
** operator is used to collect multiple keyword arguments into a dictionary.
Code
Python
def sample(**kwargs):
print("Packed keyword arguments:", kwargs)
sample(name="Anaya", age=25, country="India")
OutputPacked keyword arguments: {'name': 'Anaya', 'age': 25, 'country': 'India'}
Explanation:
**kwargs
collects keyword arguments as a dictionary.- Each key-value pair is stored in
kwargs
.
Unpacking Arguments
Unpacking allows values from an iterable (list, tuple, or dictionary) to be passed as separate arguments to a function.
1. Unpacking a List/Tuple with *
We use * to unpack elements from a list/tuple.
Example
Python
def addition(a, b, c):
return a + b + c
num = (1, 5, 10)
result = addition(*num)
print("Sum:", result)
Explanation: *numbers
unpacks numbers
into a, b, c
.
2. Unpacking a Dictionary with **
We use **
to unpack key-value pairs from a dictionary.
Example
Python
def info(name, age, country):
print(f"Name: {name}, Age: {age}, Country: {country}")
data = {"name": "geeks for geeks", "age": 30, "country": "India"}
info(**data)
OutputName: geeks for geeks, Age: 30, Country: India
Explanation: **data unpack dictionary values and assign them to parameters.
Packing and Unpacking Together
We can use Packing and Unpacking in same function.
Example
Python
def together(*args, **kwargs):
print("Positional:", args)
print("Keyword arguments:", kwargs)
together(1, 2, 3, name="geeks for geeks", age=30)
OutputPositional: (1, 2, 3)
Keyword arguments: {'name': 'geeks for geeks', 'age': 30}
Explanation:
*args
collects (1, 2, 3)
as a tuple.**kwargs
collects name="geeks for geeks"
and age=30
into a dictionary.
Difference between Packing and Unpacking
Features | Packing | Unpacking |
---|
Definition | Collects multiple values into a single variable (tuple, list, or dictionary). | Extracts values from a collection (tuple, list, or dictionary) into individual variables. |
---|
Operator Used | * (for tuples/lists), ** (for dictionaries).
| * (for lists/tuples), ** (for dictionaries).
|
---|
Purpose | Allows functions to accept a variable number of arguments. | Allows passing values dynamically to functions or variables. |
---|
Data Structure | Combine multiple values into a single entity (tuple or dictionary). | Extracts multiple values from a single entity into separate variables. |
---|
Example in function definition | def func(*args): (packs multiple positional arguments into a tuple) def func(**kwargs): (packs multiple keyword arguments into a dictionary)
| func(*list_var) (unpacks elements from a list/tuple) func(**dict_var) (unpacks key-value pairs from a dictionary)
|
---|
Example inf unction call | func(1, 2, 3, 4) → Packs (1, 2, 3, 4) into args .
| func(*[1, 2, 3, 4]) → Unpacks [1, 2, 3, 4] as separate arguments.
|
---|
Storage Format | Tuple (*args ) or Dictionary (**kwargs ). | Individual variables or function arguments. |
---|