0
0
PythonHow-ToBeginner · 3 min read

How to Unpack Dictionary in Python: Simple Guide

In Python, you can unpack a dictionary using the ** operator inside function calls or when creating new dictionaries. This spreads the key-value pairs into the new context, making it easy to merge or pass dictionary contents.
📐

Syntax

The basic syntax to unpack a dictionary uses the ** operator before the dictionary name. It can be used in function calls or when creating new dictionaries.

  • func(**dict): Passes all key-value pairs as named arguments to a function.
  • {**dict1, **dict2}: Creates a new dictionary by merging others.
python
def greet(name, age):
    print(f"Hello {name}, you are {age} years old.")

person = {"name": "Alice", "age": 30}
greet(**person)

# Merging dictionaries
info1 = {"city": "Paris"}
info2 = {"country": "France"}
full_info = {**info1, **info2}
print(full_info)
Output
Hello Alice, you are 30 years old. {'city': 'Paris', 'country': 'France'}
💻

Example

This example shows how to unpack a dictionary to pass its contents as arguments to a function and how to merge two dictionaries into one.

python
def describe_pet(name, species):
    print(f"I have a {species} named {name}.")

pet_info = {"name": "Buddy", "species": "dog"}
describe_pet(**pet_info)

# Merging dictionaries
stats1 = {"height": 180, "weight": 75}
stats2 = {"age": 25, "eye_color": "blue"}
combined_stats = {**stats1, **stats2}
print(combined_stats)
Output
I have a dog named Buddy. {'height': 180, 'weight': 75, 'age': 25, 'eye_color': 'blue'}
⚠️

Common Pitfalls

One common mistake is trying to unpack a dictionary without using the ** operator, which causes errors. Another is unpacking dictionaries with overlapping keys when merging, which overwrites earlier values silently.

Also, unpacking dictionaries into functions requires the keys to match the function's parameter names exactly.

python
def show_info(name, age):
    print(f"Name: {name}, Age: {age}")

info = {"name": "Bob", "age": 40}

# Wrong: missing ** causes TypeError
# show_info(info)  # This will raise an error

# Correct:
show_info(**info)

# Overlapping keys example
first = {"a": 1, "b": 2}
second = {"b": 3, "c": 4}
merged = {**first, **second}
print(merged)  # 'b' key value is 3, second dict overwrites first
Output
Name: Bob, Age: 40 {'a': 1, 'b': 3, 'c': 4}
📊

Quick Reference

Use **dict to unpack dictionaries in these common cases:

  • Function calls: func(**my_dict)
  • Dictionary merging: new_dict = {**dict1, **dict2}
  • Creating copies: copy = {**original}

Remember keys must match function parameters and overlapping keys overwrite in merges.

Key Takeaways

Use the ** operator to unpack dictionaries in function calls or when creating new dictionaries.
Keys in the dictionary must match function parameter names when unpacking into functions.
Merging dictionaries with ** overwrites values of duplicate keys from later dictionaries.
Always use ** before the dictionary name to unpack; omitting it causes errors.
Unpacking dictionaries makes code cleaner and helps combine or pass data easily.