In this article we will discuss how to create functions in Python that can accept variable length arguments in key value pair format.

Suppose we want to define a function that will print the provided students details. Details will be in key value pairs, where key represents the property name and value as property’s value.
It should print all the properties provided irrespective of their count i.e.

publishStudentDetails(name="Aadi")

Should print 1 property i.e. name.
publishStudentDetails(name="Jack", phone="2311")

Should print 2 properties of student i.e. name & phone.
publishStudentDetails(name="Riti", phone="3444", address="London")

Should print 3 properties of student i.e name, phone & address.

Important things is that function should accepts variable length arguments in key value pair format. Let’s define a function like this,

Define function that accepts variable length arguments in key value pair using **kwargs

In Python if we want to accept multiple key value pair arguments in a single parameter then we need to add ** before the parameter name i.e.

def publishStudentDetails(**kwargs):

Here **kwargs can accept multiple key value pair of arguments and will store them in dictionary i.e. kwargs will be of dictionary type.

Now let’s create the function using **kwargs i.e.

def publishStudentDetails(**kwargs):
    '''
    Accept variable length arguments in key value pair format.
    '''
    print(kwargs)
    print("Iterate over the arguments")
    for key, value in kwargs.items() :
        print("Student's " , key , " is ", value)


Now let’s pass variable number of key value pairs in this function,

Passing 3 key value pairs

publishStudentDetails(name="Sam", phone="1234", address="New York")

Output
Student's  phone  is  1234
Student's  address  is  New York
Student's  name  is  Sam

Passing 2 key value pairs
publishStudentDetails(name="Sam", phone="1234")

Output
Student's  phone  is  1234
Student's  name  is  Sam

Passing 1 key value pair
publishStudentDetails(name="Sam")

Output
Student's  name  is  Sam

Position of **kwargs , *args & Default Arguments

Positioning of arguments in function should be in this order,

  1. Formal Parameter
  2. *args Arguments
  3. Default Arguments
  4. **kwargs Arguments

Let’s create a function that accepts all the above mentioned types of arguments in order i.e.

def publishStudentDetails2(startMsg, endMsg, *args , collegeName="XYZ"  , **kwargs):
    ''' Function that accepts all formal arguments, *args, Default Argumnets & **kwargs '''
    print(startMsg)
    print(endMsg)
    print(collegeName)
    print(kwargs)
    print(args)

Let’s call this function
publishStudentDetails2("START", "END" , ("ss", "sdd",) , name="Jack", phone="2311", address="Delhi")

Output:
START
END
XYZ
{'name': 'Jack', 'phone': '2311', 'address': 'Delhi'}
(('ss', 'sdd'),)

Complete example is as follows,
def publishStudentDetails(**kwargs):
    '''
    Accept variable length arguments in key value pair format.
    '''
    print(kwargs)
    print("Iterate over the arguments")
    for key, value in kwargs.items() :
        print("Student's " , key , " is ", value)



def publishStudentDetails2(startMsg, endMsg, *args , collegeName="XYZ"  , **kwargs):
    ''' Function that accepts all formal arguments, *args, Default Argumnets & **kwargs '''
    print(startMsg)
    print(endMsg)
    print(collegeName)
    print(kwargs)
    print(args)
    
            
if __name__ == '__main__':
    
    publishStudentDetails(name="Sam", phone="1234", address="New York")
    publishStudentDetails(name="Riti", phone="3444", address="London")
    publishStudentDetails(name="Jack", phone="2311")
    publishStudentDetails(name="Aadi")
    
    publishStudentDetails2("START", "END" , ("ss", "sdd",) , name="Jack", phone="2311", address="Delhi")
    



Output
{'name': 'Sam', 'phone': '1234', 'address': 'New York'}
Iterate over the arguments
Student's  name  is  Sam
Student's  phone  is  1234
Student's  address  is  New York
{'name': 'Riti', 'phone': '3444', 'address': 'London'}
Iterate over the arguments
Student's  name  is  Riti
Student's  phone  is  3444
Student's  address  is  London
{'name': 'Jack', 'phone': '2311'}
Iterate over the arguments
Student's  name  is  Jack
Student's  phone  is  2311
{'name': 'Aadi'}
Iterate over the arguments
Student's  name  is  Aadi
START
END
XYZ
{'name': 'Jack', 'phone': '2311', 'address': 'Delhi'}
(('ss', 'sdd'),)