Python kwargs - Keyword arguments as dictionary

Python **kwargs is a dictionary argument.

def f(**kwargs):
    print(kwargs)
    print(type(kwargs))


f(first_name='Kyle', last_name='McDonald')

# {'first_name': 'Kyle', 'last_name': 'McDonald'}
# <class 'dict'>

kwargs is a dictionary so can be iterated in for loop. Each element of a Python dictionary is a tuple, which we can get from kwargs.items().

def f(**kwargs):
    for i in kwargs.items():
        print(i)


f(first_name='Kyle', last_name='McDonald')

# ('first_name', 'Kyle')
# ('last_name', 'McDonald')

Get only keys or values from kwargs

If items() is omitted, only keys in kwargs are taken.

def f(**kwargs):
    for i in kwargs:
        print(i)


f(first_name='Kyle', last_name='McDonald')

# first_name
# last_name

The following shows how to get all the values in kwargs.

def f(**kwargs):
    for i in kwargs.values():
        print(i)


f(first_name='Kyle', last_name='McDonald')

# Kyle
# McDonald

Python Function

Python Tutorial