
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
How to use *args and **kwargs in Python?
In Python, functions usually have a fixed number of arguments. However, there are situations where we may want to pass a variable number of arguments. In such cases, Python provides two special constructs: *args and **kwargs.
- *args allows a function to accept any number of positional arguments.
- **kwargs allows a function to accept any number of keyword arguments.
We will discuss both of these concepts deeply in this article.
What is "*args" in Python?
In Python, *args allows a function to accept any number of positional arguments. These arguments are collected into a tuple inside the function, allowing you to iterate through or manipulate them as needed.
Positional arguments are values passed to a function in the order in which the parameters are defined, without explicitly specifying the parameter names.
Example
In this example, the greet() function accepts a variable number of positional arguments (names) and prints a greeting for each name passed to it:
# 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 a function to accept any number of keyword arguments. These arguments are passed as a dictionary, where the keys are the argument names and the values are their corresponding values.
Keyword Arguments are passed using parameter names, so their order doesn't matter.
Example
In this example, print_details() function accepts any number of keyword (named) arguments and prints them as key-value pairs using a loop:
# 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
You can combine *args and **kwargs in a single function to accept both positional and keyword arguments. When used together, *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 show_info() 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 arguments from sequences (like lists or tuples) and dictionaries into positional and keyword arguments.
Example
In the following example, we are using the unpacking operators * and ** to pass a list and a dictionary to the display() function.
The list args is unpacked into individual positional arguments, and the dictionary kwargs is unpacked into individual keyword arguments, matching the function's parameters:
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
Conclusion
In this article, we explored how to use *args and **kwargs in Python to handle variable numbers of arguments.
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.