Extending Dictionary in Python [SOLVED]

Introduction

A dictionary is a changeable, unordered Python container used to hold mappings between distinct keys and values. Curly brackets () are used for dictionaries, as well as key-value pairs with commas between them (,). Each key's value is separated from it by a colon (:). Dictionaries are a very good, fast, and trackable data structure used to store data in Python using a key-pairs structure.

We can use different built-in functions to manipulate the dictionaries in python. Today, we will discuss how to extend a dictionary in Python. Let’s get started.

Advertisement

 

Extending dictionary in Python

Extending a python dictionary means merging two or more dictionaries, or adding key-paired values to the dictionary by using different methods. We will discuss all the methods one by one.

 

Method-1: Using the update() function

The update() function is used to merge two or more dictionaries. The update() function will remove the duplicates if any. Let’s say, we have two different dictionaries and we are interested in extending the two dictionaries into one, we will use the update() function. Let’s see an example:

dict1 = {'Name' : 'Jhon', 'Age' : 20}
dict2 = {'GPA' : 3.7, 'Address': 'XYZ'}
dict1.update(dict2)
print(dict1)

Output:

{'Name': 'Jhon', 'Age': 20, 'GPA': 3.7, 'Address': 'XYZ'}

We have defined two dictionaries and merged them using the update() function. Note that we have merged dict2 with dict1. Now, dict1 will also have the values of dict2.

 

Method-2: Using the ** operators

We can also merge two or more two dictionaries by using the (**) operators. Let’s see this in an example:

dict1 = {'Name' : 'Jhon', 'Age' : 30}
dict2 = {'GPA' : 3.7, 'Address' : 'XYZ'}
dict3 = {**dict1, **dict2}
print(dict3)

Output:

Advertisement
{'Name': 'Jhon', 'Age': 20, 'GPA': 3.7, 'Address': 'XYZ'} 

In the above example, we have used (**) operators to merge the two initialized dictionaries. If you are not using the dict() function then you have to put these operators with both dictionaries.

 

Method-3: Using dict() function

We can also extend the dictionary using the dict() function. This is a very powerful function and can be used to initialize dictionaries. Using this function, we use also use the (**) operators but with only one dictionary. Let’s understand this well with an example:

dict1 = {'Name' : 'Jhon', 'Age' : 30}
dict2 = {'GPA' : 3.7, 'Address' : 'XYZ'}
dict3 = dict(dict1, **dict2)
print(dict3)

Output:

{'Name': 'Jhon', 'Age': 20, 'GPA': 3.7, 'Address': 'XYZ'} 

In the above code snippet, we have used the dict() function. Here, we are using the (**) but with only one dictionary.

 

Method-4: Using union operator (|)

We can also use the union to extend dictionaries. This is the simplest way. We use the union operator (|) between two initialized dictionaries, this will merge them. Let’s take an example:

dict1 = {'Name' : 'Jhon', 'Age' : 30}
dict2 = {'GPA' : 3.7, 'Address' : 'XYZ'}
dict3 = dict1 | dict2
print(dict3)

Output:

Advertisement
{'Name': 'Jhon', 'Age': 20, 'GPA': 3.7, 'Address': 'XYZ'} 

As you can see that we have simply placed the union operator and it extended the two dictionaries very well. That’s all for today.

Thank you!

 

Summary

We saw how different methods can be applied to extend dictionaries in python. We implemented different functions in python and extended two dictionaries together. There are many ways to do that and some of the most used ones are explained in this article.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X