
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use args and kwargs in Python
We can use *args and **kwargs in Python to pass a variable number of arguments to a function.
In Python, functions usually have a fixed number of arguments. But sometimes, we may need to pass a variable number of arguments. This is where *args and **kwargs come in.
Using *args allows a function to accept any number of positional arguments, while **kwargs allows it to accept any number of keyword arguments -
- Positional arguments are passed to a function in the same order as the parameters are defined.
- Keyword arguments are passed by explicitly naming the parameter, so the order doesn't matter.
What is "*args" in Python?
In Python, *args allows a function to accept any number of positional arguments. It collects extra arguments into a tuple. This is helpful when we don't know in advance how many arguments will be passed to the function.
Example
In the following example, we are using *args to allow the function to accept any number of names. The function loops through each name and prints a greeting message for all provided arguments -
# Function using *args def greet(*args): for name in args: print("Hello", name) greet("Rohan", "Sakshi") greet("Tom", "Jerry")
Following is the output of the above code -
Hello Rohan
Hello Sakshi
Hello Tom
Hello Jerry
What is "**kwargs" in Python?
In Python, **kwargs allows you to pass any number of keyword arguments. The function receives them as a dictionary, where the keys are the argument names and the values are the argument values.
This is useful when we want to handle named arguments dynamically.
Example
In this example, we are using **kwargs to pass a variable number of keyword arguments to the function. The function loops through each key-value pair and prints the details provided -
# Function using **kwargs def print_details(**kwargs): for key, value in kwargs.items(): print(key, ":", value) print_details(name="Rohan", age=25) print_details(city="Delhi", country="India", zip=75000)
The result obtained is as follows -
name : Rohan age : 25 city : Delhi country : India zip : 75000
Using "*args" and "**kwargs" Together
We can use both *args and **kwargs in the same function to accept all types of arguments. In such cases, *args must appear before **kwargs in the function definition.
Example
In the example below, we are using both *args and **kwargs to pass multiple positional and keyword arguments to the function.
The function prints the positional arguments as a tuple and the keyword arguments as a dictionary -
def show_info(*args, **kwargs): print("Positional arguments:", args) print("Keyword arguments:", kwargs) show_info("Rohan", "Samaira", age=30, country="India")
We get the output as shown below -
Positional arguments: ('Rohan', 'Samaira') Keyword arguments: {'age': 30, 'country': 'India'}
Using "*" and "**" to Unpack Arguments
We can also use * and ** when calling a function to unpack values from a list or dictionary into arguments.
Example
In the following example, we are using the unpacking operators * and ** to pass a list and a dictionary to a function.
The list args is unpacked into positional arguments, and the dictionary kwargs is unpacked into keyword arguments for the display() function -
def display(a, b, c): print(a, b, c) # Unpacking a list args = [1, 2, 3] display(*args) # Unpacking a dictionary kwargs = {"a": "x", "b": "y", "c": "z"} display(**kwargs)
After executing the above code, we get the following output -
1 2 3 x y z
When to Use *args and **kwargs
You can use *args when -
- You want to accept multiple positional arguments.
- You are writing wrapper functions like decorators.
You can use **kwargs when -
- You want to accept multiple keyword arguments.
- You want to pass configuration or options to functions.