In this post, we’ll look at how to write Python functions that accept variable-length arguments in key-value pair format.
Functions that accept Variable length Key Value pair as Arguments
- Kwargs
- Variable Length Functions
- Writing Function which accepts variable length arguments in key value pair using **kwargs
1)Kwargs
In Python function definitions, the special syntax **kwargs is used to transfer a keyworded, variable-length argument set. We call ourselves kwargs with a double star. The explanation for this is that the double star helps one to push keyword arguments through (and any number of them).
- A keyword statement is to give the variable a name before entering it.
- You can think of the kwargs as a dictionary mapping every keyword to the value we pass. This is why there appears to be no order in which we are iterating through the kwargs.
2)Variable Length Functions
Assume we want to define a function that prints the given student information. Details will be presented in key value pairs, with key representing the property name and value representing the property’s value.
It should print all of the given properties, regardless of their count.
printDetails(name = "Vishal")
Above one should only print one property, namely the name.
printDetails(name = "Vishal", pincode = "983992")
Above one should print two properties , namely the name and pincode
printDetails(name = "Vishal", phone = "333444",address = "NewYork")
Above one should print three properties, namely the name , pincode and address.
It is important that the function accepts variable length arguments in key value pair format.
3)Writing Function which accepts variable length arguments in key value pair using **kwargs
In Python, we must add ** before the parameter name if we want to allow multiple key value pair arguments in a single parameter.
**kwargs can accept multiple key value pair arguments and store them in a dictionary, indicating that kwargs are of the dictionary type.
Let’s make the function with **kwargs now.
Below is the implementation:
def printDetails(**kwargs): # Variable-length arguments in key-value pair format are accepted. print("Printing all the arguments", kwargs) print("Traverse the arguments") for key, value in kwargs.items(): print("person's ", key, " = ", value) print() # passing key's and values to printDetails function # passing one argument printDetails(name="Vishal") # passing two arguments printDetails(name="Akash", pincode=533333) # passing three arguments printDetails(name="Vikram", pincode=213442, adress="NewYork")
Output:
Printing all the arguments {'name': 'Vishal'} Traverse the arguments person's name = Vishal Printing all the arguments {'name': 'Akash', 'pincode': 533333} Traverse the arguments person's name = Akash person's pincode = 533333 Printing all the arguments {'name': 'Vikram', 'pincode': 213442, 'adress': 'NewYork'} Traverse the arguments person's name = Vikram person's pincode = 213442 person's adress = NewYork
Related Programs:
- python program to create a dictionary with key as first character and value as words starting with that character
- get first key value pair from a python dictionary
- python program to add a key value pair to the dictionary
- python dictionary append add update key value pairs
- python iterate over dictionary all key value pairs
- python how to sort a dictionary by key or value
- python print all key value pairs of a dictionary