DEV Community

qing
qing

Posted on

Quick Python Tip: Unpack Dictionaries Directly Into Function Arguments (2026)

Quick Python Tip: Unpack Dictionaries Directly Into Function Arguments

When working with functions in Python, you might find yourself needing to pass a dictionary's key-value pairs as arguments. This can be tedious, especially if the dictionary has many items. Luckily, Python provides a convenient way to unpack dictionaries directly into function arguments using the **kwargs syntax.

Imagine you have a function that takes several keyword arguments, and you have a dictionary with the same keys. Instead of manually passing each key-value pair, you can use the ** operator to unpack the dictionary into the function call.

Here's a short example:

def greet(name, age, city):
    print(f"Hello, {name} from {city}! You are {age} years old.")

person = {"name": "John", "age": 30, "city": "New York"}
greet(**person)  # Output: Hello, John from New York! You are 30 years old.
Enter fullscreen mode Exit fullscreen mode

As you can see, the **person syntax allows us to pass the dictionary's key-value pairs as keyword arguments to the greet function. This makes the code more concise and easier to read.

Takeaway: You can simplify your Python code by using **kwargs to unpack dictionaries directly into function arguments.


Follow me on Dev.to for daily Python tips and quick guides!

Top comments (0)