Last Updated : 18 Feb, 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 ArgumentsPacking allows multiple values to be combined into a single parameter using * (for tuples/lists) and ** (for dictionaries).
The *
operator allows us to pass multiple arguments to a function and pack them into a tuple.
def sample(*args):
print("Packed arguments:", args)
sample(1, 2, 3, 4, "geeks for geeks")
Packed arguments: (1, 2, 3, 4, 'geeks for geeks')
Explanation:
*args
packs all arguments into a tuple.** 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")
Packed keyword arguments: {'name': 'Anaya', 'age': 25, 'country': 'India'}
Explanation:
**kwargs
collects keyword arguments as a dictionary.kwargs
.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
.
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)
Name: geeks for geeks, Age: 30, Country: India
Explanation: **data unpack dictionary values and assign them to parameters.
Packing and Unpacking TogetherWe 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)
Positional: (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.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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4