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.

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.

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.

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.

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:
- Flip Y-Axis Label in Matplotlib using Python
- Invert the Y-Axis in Matplotlib imshow
- Create Two Y Axes Bar Plot in Matplotlib
- Invert the Y-Axis in 3D Plot using Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.