Related Articles

Related Articles

Functions that accept variable length key value pair as arguments
  • Last Updated : 29 Dec, 2020

To pass a variable-length key-value pair as an argument to a function, Python provides a feature called **kwargs.
kwarg stands for Keyword arguments. It proves to be an efficient solution when one wants to deal with named arguments in their function.

Syntax:

def functionName(**anything):
    statement(s)

Note: adding ‘**‘ to any term makes it a kwarg parameter. It accepts keywords as arguments.

Example #1: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# using kwargs
# in functions
  
  
def printKwargs(**kwargs):
    print(kwargs)
  
  
# driver code
if __name__ == "__main__":
    printKwargs(Argument_1='gfg', Argument_2='GFG')

chevron_right


Output:



{'Argument_1': 'gfg', 'Argument_2': 'GFG'}

Example #2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# using kwargs
# in functions
  
  
def printValues(**kwargs):
    for key, value in kwargs.items():
        print("The value of {} is {}".format(key, value))
  
  
# driver code
if __name__ == '__main__':
    printValues(abbreviation="GFG", full_name="geeksforgeeks")

chevron_right


Output:

The value of abbreviation is GFG
The value of full_name is geeksforgeeks

Example #3:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# using kwargs
# in functions
# to concatenate
  
  
def concatenate(**arguments):
    # initialising empty string
    final_str = ""
      
    # Iterating over the Python kwargs
    # dictionary
    for elements in arguments.values():
        final_str += elements
    return final_str
  
  
# driver code
if __name__ == '__main__':
    print(concatenate(a="g", b="F", c="g"))

chevron_right


Output:

gFg

Example #3:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# using kwargs
# to multiply
  
  
def multiply(**kwargs):
    
    # initialisng answer
    answer = 1
      
    # Iterating over the Python kwargs
    # dictionary
    for elements in kwargs.values():
        answer *= elements
    return answer
  
  
# driver code
if __name__ == '__main__':
    print(multiply(a=1, b=2, c=3, d=4, e=5))

chevron_right


Output:

120

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :