Skip to main content

Python - How To Merge Two Dictionaries

 PythonHow to merge two dictionaries

As we know If you are working as Python developer, data analytics or data scientists for any organisation then it is very important for you to know how to merge two dictionaries.

There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Python. In this article, we will discuss a few ways of merging dictionaries. 

Using the method update ()

By using the method update() in Python, one list can be merged into another. But in this, the second list is merged into the first list and no new list is created. It returns None

 

Example

# Python code to merge dict using update() method def Merge(dict1, dict2):     return(dict2.update(dict1))      
# Driver code dict1 = {'a': 20, 'b': 8} dict2 = {'d': 6, 'c': 4}
 
# This return None print(Merge(dict1, dict2))
 
# changes made in dict2 print(dict2)



Output
None
 {'c': 4, 'a': 20, 'b': 8, 'd': 6}

Using ** in Python


This is generally considered a trick in Python where a single expression is used to merge two dictionaries and stored in a third dictionary. The single expression is **. This does not affect the other two dictionaries. ** implies that an argument is a dictionary. Using ** [double star] is a shortcut that allows you to pass multiple arguments to a function directly using a dictionary. 
Using this we first pass all the elements of the first dictionary into the third one and then pass the second dictionary into the third. This will replace the duplicate keys of the first dictionary.

Example 

# Python code to merge dict using a single
# expression def Merge(dict1, dict2):     res = {**dict1, **dict2}i     return res      
# Driver code dict1 = {'a': 20, 'b': 8} dict2 = {'d': 6, 'c': 4} dict3 = Merge(dict1, dict2) print(dict3)

Output
{'b': 8, 'a': 20, 'c': 4, 'd': 6}



Comments

Popular posts from this blog

Cloud Computing

 CLOUD COMPUTING The cloud is a generic term used for Internet. Cloud computing is Internet – based  Computing , Whereby Shared Resources , Software, and Information are provided  to Computer and Other devices on demand, Like the electricity grid….. Types Of Clouds There are different types of Clouds that you can subscribe to depending on your choice (Needs)….. There are four deployment models of Clouds are: 1. Private Clouds 2. Public Clouds 3. Community Clouds 4. Hybrid Clouds Private Clouds   These are the clouds for exclusive use by a single Organization and Typically Controlled, managed and hosted in private data  But a private Clouds remains for the exclusive use of one organization. For example:- An example of a private cloud deployment is where you maintain your own servers and infrastructure that hosts your applications and data. ... A Hybrid model is where you also have some applications or servers based on business need running in a public cloud infrastructure like Microsoft

IoT - Internet of Things

IoT ( Internet Of Things) The Internet of things (IoT) is a system of interrelated computing devices, mechanical and digital machines provided with unique identifiers (UIDs) and the ability to transfer data over a network without requiring human-to-human or human-to-computer interaction. IoT in simple words The Internet of Things is simply "A network of Internet connected objects able to collect and exchange data." It is commonly abbreviated as IoT. In a simple way to put it, You have "things" that sense and collect data and send it to the internet. Uses of IoT IoT technology is most synonymous with products pertaining to the concept of the "smart home", covering devices and appliances (such as lighting fixtures, thermostats, home security systems and cameras, and other home appliances) that support one or more common ecosystems, and can be controlled via devices associated with that ecosystem, such as smartphones and smart speakers.