Packing and Unpacking Arguments in Python
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:
def sample(*args):
print("Packed arguments:", args)
sample(1, 2, 3, 4, "geeks for geeks")
Output
Packed arguments: (1, 2, 3, 4, 'geeks for geeks')
Explanation:
- The function takes any number of arguments.
- The
*argspacks all arguments into a tuple.
2. Packing with **kwargs
** operator is used to collect multiple keyword arguments into a dictionary.
Code
def sample(**kwargs):
print("Packed keyword arguments:", kwargs)
sample(name="Anaya", age=25, country="India")
Output
Packed keyword arguments: {'name': 'Anaya', 'age': 25, 'country': 'India'}
Explanation:
**kwargscollects 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
def addition(a, b, c):
return a + b + c
num = (1, 5, 10)
result = addition(*num)
print("Sum:", result)
Output
Sum: 16
Explanation: *numbers unpacks numbers into a, b, c.
2. Unpacking a Dictionary with **
We use ** to unpack key-value pairs from a dictionary.
Example
def info(name, age, country):
print(f"Name: {name}, Age: {age}, Country: {country}")
data = {"name": "geeks for geeks", "age": 30, "country": "India"}
info(**data)
Output
Name: 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
def together(*args, **kwargs):
print("Positional:", args)
print("Keyword arguments:", kwargs)
together(1, 2, 3, name="geeks for geeks", age=30)
Output
Positional: (1, 2, 3)
Keyword arguments: {'name': 'geeks for geeks', 'age': 30}
Explanation:
*argscollects(1, 2, 3)as a tuple.**kwargscollectsname="geeks for geeks"andage=30into 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 |
|
|
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 |
|
|
Example inf unction call |
|
|
Storage Format | Tuple ( | Individual variables or function arguments. |