Updated June 14, 2023
Introduction to Python kwargs
In this article, we discuss the kwargs concept in Python. In Python, args is a variable with one star, which passes a variable number of the non-keyworded argument list, whereas kwargs have two stars that pass a variable number of the keyworded argument list to the function. These *args and **kwargs make the function flexible. In Python, we use kwargs, which are keyword arguments used when we provide a name to a variable as we pass it to the function. We use kwargs when we want to handle named arguments with a variable-length argument dictionary in a function.
Working with kwargs in Python with examples
In this section, when we are unsure how many arguments are needed in the program, we use kwargs with two stars (**) before the parameter name. In Python, when using kwargs, we declare it with two stars (**). Now let us see the demonstration of kwargs in the below example.
Example #1
Code:
print("Program to demonstrate **kwargs for variable number of keywords:")
print("\n")
def concatenate(**kwargs):
r = ""
for arg in kwargs.values():
r += arg
return r
print("The concatenated value is displayed as follows:")
print(concatenate(a="Educba", b="Training", c="Institue"))
Output:
In the above program, we can see that we have defined a function using the argument variable as kwargs with two stars before it. So when we call this function to concatenate(), which will iterate through the given kwargs dictionary a= “Educba”, b= “Training”, c= “Institue” using “for” loop. Then it prints all these words together, as shown in the output and screenshot above.
Now we will see another use of **kwargs. Let’s see below a function that creates using a dictionary of names.
Example #2
Code:
print("Another use of **kwargs:")
print("\n")
def print_values(**kwargs):
for key, value in kwargs.items():
print("The value of {} is {}".format(key, value))
print_values(my_name="Rahul", your_name="Ratan")
Output:
In the above program, we have created a dictionary using **kwargs. As we know dictionary can be unordered; the output might display the name first “Rahul” or with another name, “Ratan” so the dictionary has no order to display the output. This can be seen in the above screenshot.
Therefore when we use **kwargs, we can pass any number of arguments, and these can also have the output, which has a dictionary created and will display an unordered output again. By this, we can say that **kwargs are flexible to use with keyword arguments in the program. Therefore, we should remember that we should properly keep the order of the parameters when creating any function, and we can use the same order in the function call else. If we do not follow the order, we will get an error. The use of **kwargs is very simple and provides readability, but we should use it with care as it provides the key: value pairs to follow the order.
In Python, the known value within the argument list will remain small whenever the developers or users need a number of inputs without a fix. Let us see below how *args and *kwargs are used. Let us demonstrate below with examples.
Example #3
Below is the program that uses *args to pass the elements to the function in an iterable variable.
Code:
print("Program to demonstrate the *args is as follows:")
def func(a, b, c):
print(a, b, c)
a = [1,2,3]
func(*a)
Output:
This program utilizes *args to break the list “a” into three elements. We should also note that the above program works only when the number of parameters of a function is the same as the number of elements in the given iterable variable (here, it is list “a”).
Now we will see, about **kwargs, to call the function as the above program with *args. Let us demonstrate using the below example.
Example #4
Code:
print("Program to demonstrate the **kwargs used in function call is as follows:")
def func(a, b, c):
print(a, b, c)
a = {'a': "one", 'b': "two", 'c': "three" }
func(**a)
Output:
In the above program, we are using **kwargs with the name variable as “a” which is a list. Again the above program to work, we need to note that the name of the parameters passed to the function must also have the same name in the dictionary where these act as the keys. And we should also note that the number of arguments should be the same as the number of keys in the dictionary.
In the above section, we observed that args, which employs a single star (), generates the list containing positional arguments defined from the provided function call. Whereas we saw in the above **kwargs, which has a double star (**) which creates a dictionary with keys as each element whose contents can be keyword arguments after those defined from the function call. Hence *args and **kwargs are standard conventions to catch positional and keyword arguments, respectively. We should also note that when we use these two types of arguments in one function, we cannot place or write **kwargs before *args, or we will receive an error.
Conclusion
This article concludes that **kwargs is a keyword argument length list when creating the function with parameters. In this, we saw simple examples of **kwargs. We also saw the use of **kwargs when we were unsure of how many parameters to use we can use kwargs. Then we also saw the difference between *args and **kwargs and how they are used in the function call. In this article, we also saw some important notes to remember, such as we need to pass the same number of arguments with the same number of elements when calling the function. We also saw **kwargs creates a dictionary that displays an unordered element when executed.
Recommended Articles
We hope that this EDUCBA information on “Python kwargs” was beneficial to you. You can view EDUCBA’s recommended articles for more information.