How to Extend a Dictionary in Python

Recently, I was working on a Python project where I had to merge multiple data sources into a single dictionary. The challenge was to extend an existing dictionary with new key-value pairs efficiently without losing any data.

If you’ve ever worked with large datasets in Python, you know how important it is to manage dictionaries effectively. In this article, I’ll show you four simple and practical ways to extend a dictionary in Python. These are methods I personally use in my day-to-day development work.

Python Extend Dictionary

Multiple ways exist to extend the existing dictionary with the new key pair below, which are discussed section by section.

Method 1: Use update() Method in Python

The update() method is a direct way to add key-value pairs from one dictionary to another. This method modifies the original dictionary in place.

For example, look at and run the code below.

# Create a dictionary called employee_data with two key-value pairs
employee_data = {'name': 'Rock', 'age': 28}

# Create another dictionary called new_data with two key-value pairs
new_data = {'department': 'HR', 'location': 'New York'}

# Update the employee_data dictionary with the key-value pairs from new_data
employee_data.update(new_data)

# Print the updated employee_data dictionary
print(employee_data)

You can see the output in the screenshot below.

Python Extend Dictionary Using update() Method

The new_data is added to the dictionary “employee_data”, as shown in the above picture.

Method 2: Use Python Dictionary Unpacking

Dictionary unpacking using the ** operator is another efficient method of extending dictionaries. Using this method, you can create a new dictionary containing the combined key-value pairs.

Take the above section as an example.

# Create a dictionary called employee_data with two key-value pairs
employee_data = {'name': 'Rock', 'age': 28}

# Create another dictionary called new_data with two key-value pairs
new_data = {'department': 'HR', 'location': 'New York'}

# Merge the two dictionaries using dictionary unpacking
# The double asterisks (**) unpack the key-value pairs from each dictionary
# and merge them into a new dictionary called merged_data
merged_data = {**employee_data, **new_data}

# Print the merged_data dictionary
print(merged_data)

You can see the output in the screenshot below.

Python Extend Dictionary Using Dictionary Unpacking

The operator ** extends the dictionary “employee_data” as shown in the above picture.

Method 3: Use a For Loop in Python

For a more manual approach, you can use a for loop to iterate over the items in one dictionary and add them to another.

Let’s see an example of an employee_data dictionary.

# Create a dictionary called employee_data with two key-value pairs
employee_data = {'name': 'James', 'age': 28}

# Create another dictionary called new_data with two key-value pairs
new_data = {'department': 'HR', 'location': 'New York'}

# Iterate over the key-value pairs in new_data
for key, value in new_data.items():
    # Add the key-value pair from new_data to employee_data
    employee_data[key] = value

# Print the updated employee_data dictionary
print(employee_data)

You can see the output in the screenshot below.

Python Extend Dictionary Using For Loop

Using the for loop, the new_data key-value pair is added to the dictionary from the end, which means the dictionary is extended with new data.

Method 4: Use Python Dictionary Comprehension

Dictionary comprehension allows you to create a new dictionary by concisely specifying the keys and values. This method is beneficial for conditional extensions.

For example, understand and run the code below.

# Create a dictionary called employee_data with two key-value pairs
employee_data = {'name': 'Vincent', 'age': 28}

# Create another dictionary called new_data with two key-value pairs
new_data = {'department': 'HR', 'location': 'New York'}

# Merge the two dictionaries using a dictionary comprehension
# This creates a new dictionary called extended_data
# by iterating over the key-value pairs in employee_data and new_data
extended_data = {key: value for d in (employee_data, new_data) for key, value in d.items()}

# Print the extended_data dictionary
print(extended_data)

You can see the output in the screenshot below.

Python Extend Dictionary Using Dictionary Comprehension

From the above picture, you can see that the dictionary is extended using dictionary comprehension.

Extending dictionaries in Python is something I do almost daily, whether I’m merging configuration files, combining API responses, or updating user profiles.

Each method we discussed, update(), unpacking, loops, and comprehensions. An operator has its own advantages depending on your use case.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.