← All lessons

**kwargs: named arguments

Sign in · Sign up

Lesson 6.5

**kwargs collects named arguments into a dictionary:

def profile(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

profile(name="Mia", age=14)

With two stars the function takes any number of key=value arguments, and inside it is an ordinary dictionary: kwargs["name"].

The check sets the dictionary info. Write a function profile(**kwargs) that prints every pair as <key>: <value>, and call it with profile(**info).

Hint

**kwargs collects all the named arguments into a dictionary.

The function takes ** (two stars). Inside, loop over items() and print the pairs key: value. The call is profile(**info): two stars at the call as well, otherwise the dictionary comes in as one argument.