Dictionaries Merge in Python

Home /

Table of Contents

What is a dictionary in python?

In Python, a dictionary is a collection of key-value pairs. It is an unordered and mutable data type, which means that the items in a dictionary can be changed, and the order of the items is not guaranteed to be the same as the order in which they were added.

A dictionary is defined by curly braces {} and key-value pairs are separated by colons. Here’s an example of how to create a dictionary in Python:

Python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

The keys in a dictionary must be unique and immutable, meaning they can’t be changed after they’re created. Common types of keys include strings, numbers, and tuples. The values in a dictionary can be any type of data, including other dictionaries.

You can access the values in a dictionary by using their keys, like this:

Python
print(my_dict['name'])  # Output: 'John'

You can also add, modify or delete key-value pairs in a dictionary using the following methods:

  • ‘my_dict[key]’ = value’: adds or modifies a key-value pair in the dictionary
  • ‘del my_dict[key]’: delete

Merge two dictionary

In Python, you can merge two dictionaries in a single expression using the ‘update()’ method or the ‘**’ operator. The ‘update()’ method: This method updates the first dictionary with the key-value pairs from the second dictionary. You can use the ‘update()’ method on the first dictionary and pass the second dictionary as an argument.

1. The ‘update()’ method: This method updates the first dictionary with the key-value pairs from the second dictionary. You can use the ‘update()’ method on the first dictionary and pass the second dictionary as an argument.

Python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict1.update(dict2)
print(dict1)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

2. The ‘**’ operator: This operator can also be used to merge two dictionaries, it’s also known as the “unpacking” operator. You can use the ‘**’ operator on the first dictionary and pass the second dictionary as an argument.

Python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = {**dict1, **dict2}
print(merged_dict)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

3. You can also use the dict() constructor and pass a list of key-value pairs, the key-value pairs can be from multiple dictionaries.

Python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = dict(list(dict1.items()) + list(dict2.items()))
print(merged_dict)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

In python 3.5+ you can use the | operator to join two dictionaries

Python
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = dict1 | dict2
print(merged_dict)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Please keep in mind that if both dictionaries have the same keys, the second dictionary will overwrite the values of the first dictionary.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article