Extending Dictionary in Python [SOLVED]


Python

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.

 

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:

{'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:

{'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.

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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 send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment