Merging or Concatenating Two Dictionaries in Python Last Updated : 01 Nov, 2025 Comments Improve Suggest changes 47 Likes Like Report Given two dictionaries, the task is to merge them into a single dictionary that contains all key-value pairs. For example:Input: d1 = {'x': 1, 'y': 2} and d2 = {'y': 3, 'z': 4}Output: {'x': 1, 'y': 3, 'z': 4}Here, both dictionaries are combined. If the same key exists in both, the value from the second dictionary replaces the value from the first. Let’s explore different methods to merge or concatenate two dictionaries in Python.Using | Operator (Python 3.9+)| operator introduced in Python 3.9 can be used to merge dictionaries. It creates a new dictionary without modifying the original dictionary. Python d1 = {'x': 1, 'y': 2} d2 = {'y': 3, 'z': 4} d3 = d1 | d2 print(d3) Output:{'x': 1, 'y': 3, 'z': 4}Explanation:| operator combines d1 and d2 into d1 new dictionary d3.Duplicate keys are handled by keeping the value from the dictionary on the right (d2).Using Dictionary Unpacking (**)Dictionary unpacking allows us to merge dictionaries into a new one. Python d1 = {'x': 1, 'y': 2} d2 = {'y': 3, 'z': 4} d3 = {**d1, **d2} print(d3) Output{'x': 1, 'y': 3, 'z': 4} Explanation:**d1 and **d2 unpack the key-value pairs of d1 and d2 into the new dictionary d3.Keys from d2 overwrite duplicates from d1.Using update() update() method can be used to merge dictionaries. It modifies one dictionary by adding or updating key-value pairs from another. Python d1 = {'x': 1, 'y': 2} d2 = {'y': 3, 'z': 4} d1.update(d2) print(d1) Output{'x': 1, 'y': 3, 'z': 4} Explanation:d1.update(d2) adds all key-value pairs from d2 to d1.If a key exists in both d1 and d2 (e.g., 'y'), the value from d2 (3) replaces the value in d1 (2).Note: This method modifies the original dictionary (d1).Using LoopWe can use a loop (for loop) to merge dictionaries. Python d1 = {'x': 1, 'y': 2} d2 = {'y': 3, 'z': 4} d3 = d1.copy() for key, value in d2.items(): d3[key] = value print(d3) Output{'x': 1, 'y': 3, 'z': 4} Explanation:d1.copy() creates a shallow copy of d1 to preserve the original dictionary.for loop iterates through each key-value pair in d2 and adds or updates it in d3.Related Articles:Merging or Concatenating two Dictionaries in PythonPython Dictionary Create Quiz Python | Merging two Dictionaries Comment C chinmoy lenka Follow 47 Improve C chinmoy lenka Follow 47 Improve Article Tags : Python python-dict Python dictionary-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like