Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, you can unpack arguments from iterable objects (like lists, tuples, and dictionaries) when calling a function. This is useful when you want to pass a collection of values to a function as individual arguments. In this tutorial, we will learn how to unpack arguments from different data structures and pass them to functions.
You can use the asterisk (*) operator to unpack arguments from a list or tuple when calling a function.
Example:
def add(a, b, c): return a + b + c numbers = [1, 2, 3] result = add(*numbers) print(result) # Output: 6
In this example, we have a function add
that takes three arguments and returns their sum. We have a list numbers
containing three values. By using the asterisk operator when calling the function, we can unpack the values from the list and pass them as individual arguments.
You can use the double asterisk (**) operator to unpack keyword arguments from a dictionary when calling a function.
Example:
def greet(name, age): print(f"Hello, {name}! You are {age} years old.") person = {"name": "Alice", "age": 30} greet(**person) # Output: Hello, Alice! You are 30 years old.
In this example, we have a function greet
that takes two keyword arguments name
and age
. We have a dictionary person
containing the keys and values that correspond to these arguments. By using the double asterisk operator when calling the function, we can unpack the key-value pairs from the dictionary and pass them as keyword arguments.
You can mix both positional and keyword argument unpacking when calling a function.
Example:
def print_info(name, age, country): print(f"{name} is {age} years old and lives in {country}.") args = ["Alice", 30] kwargs = {"country": "USA"} print_info(*args, **kwargs) # Output: Alice is 30 years old and lives in USA.
In this example, we have a function print_info
that takes three arguments. We have a list args
containing two positional arguments, and a dictionary kwargs
containing one keyword argument. By using both the asterisk and double asterisk operators when calling the function, we can unpack the arguments from the list and the key-value pairs from the dictionary.
In summary, you can use the asterisk (*) operator to unpack positional arguments from lists and tuples, and the double asterisk (**) operator to unpack keyword arguments from dictionaries when calling functions in Python. This allows you to pass collections of values as individual arguments to functions.
How to use argument unpacking in Python functions:
def add_numbers(a, b): return a + b values = (3, 5) result = add_numbers(*values) print(result) # Output: 8
Single and double asterisk unpacking in Python:
*
) is used for unpacking iterable objects, while double asterisk (**
) is used for unpacking dictionaries.def print_values(*args, **kwargs): print(args) print(kwargs) print_values(1, 2, 3, name='Alice', age=25) # Output: # (1, 2, 3) # {'name': 'Alice', 'age': 25}
Unpacking iterable arguments in Python functions:
*
) before the iterable.def print_values(a, b, c): print(a, b, c) values = [1, 2, 3] print_values(*values) # Output: 1 2 3
Dictionary unpacking in Python function calls:
**
) allows passing key-value pairs as keyword arguments.def print_person_info(name, age): print(name, age) person_info = {'name': 'Bob', 'age': 30} print_person_info(**person_info) # Output: Bob 30
**Using *args and kwargs for argument unpacking:
*args
collects positional arguments as a tuple, and **kwargs
collects keyword arguments as a dictionary.def print_args_and_kwargs(*args, **kwargs): print(args) print(kwargs) print_args_and_kwargs(1, 2, 3, name='Alice', age=25) # Output: # (1, 2, 3) # {'name': 'Alice', 'age': 25}
Passing variable-length argument lists with unpacking in Python:
*args
and **kwargs
for flexible function signatures.def print_values(*args, **kwargs): print(args) print(kwargs) print_values(1, 2, 3, name='Alice', age=25) # Output: # (1, 2, 3) # {'name': 'Alice', 'age': 25}
Type hints for functions with unpacked arguments in Python:
*args
and **kwargs
to specify the expected types for variable-length arguments.from typing import List, Dict def process_data(*args: List[int], **kwargs: Dict[str, str]): # Function logic here pass
Combining positional and keyword arguments with unpacking:
def print_info(name, age, city): print(name, age, city) person_info = {'name': 'John', 'age': 28} print_info(**person_info, city='New York') # Output: John 28 New York
Function chaining with argument unpacking in Python:
def add_and_square(x, y): return x + y, (x + y) ** 2 def double(value): return value * 2 result_add, result_square = add_and_square(2, 3) final_result = double(result_square) print(final_result) # Output: 50