Add in Dictionary Python
Dictionary in python:
Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The information is kept in key-value pair format using a Python dictionary. The mutable data structure is what it is. The dictionary is made using the component’s Keys and Values. Keys must only contain one component. Any value, including a list, tuple, integer, etc.
d = {
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value pair corresponds to a key and its corresponding value. A collection of key-value pairs can be defined as a dictionary by covering it in curly braces (). Each key and the value it corresponds to are separated by a colon (:):
CODE:
mydict =dict([('rohith','anurag'),('vishnu','charan'),('sandeep','nivas'),('varma','dhanush')
,('srikar','venkat')])
print(mydict)
OUTPUT:

Creation of Dictionary:
Curly brackets are the simplest method for creating a Python dictionary. However there are other approaches as well. With numerous key-value pairs enclosed in curly brackets and a colon separating each key from its value, the dictionary could be created (:). Below is provided the syntax to describe the dictionary.
CODE:
mydict =dict([('gon','freece'),('eren','yeager'),('naruto','uzumaki'),('monkey','d. luffy')
,('anos','voldigoad')])
print(mydict)
OUTPUT:
{'gon': 'freece', 'eren': 'yeager', 'naruto': 'uzumaki', 'monkey': 'd. luffy', 'anos': 'voldigoad'}
The dictionary's entries appear in the order in which they were defined. But in terms of getting them back, that is irrelevant. No numerical index is used to access dictionary elements. Although the sequence of items inside a dictionary is not necessary for access, Python does ensure that the sequence of items inside a dictionary is maintained. When items are displayed, they do so in the defined order, and the keys are iterated in the same order. A dictionary is expanded at the very end. If items have been deleted, the remaining items' order is preserved.
A simple code for the inbuilt dictionary function uses the clear function to clear the entire dictionary elements.
CODE:
mydict = dict(eren = 'yeager', mikasa = 'ackerman', armin = 'arlet', levi = 'ackerman')
print(mydict.clear())
print(mydict)
OUTPUT:
None
{}
Similarly, many in-built functions are available in python dictionaries which are d.get(), d.items(), d.keys(), d.values(), d.pop(), d.popitem(), and d.update().
Adding Two Dictionaries:
A Python dictionary data structure holds all of the components in key-value pairs. Every key-value pair corresponds to the keys' associative values. As a result, it is also referred to as the Python Dictionary's associative array. Curly braces are used to separate each dictionary element. Furthermore, each key and its associated value are separated by a colon (:) symbol between those key-value pairs. The Python program’s dictionary components can be dynamically changed and arranged in any order. This topic will explore various Python dictionary methods for merging two dictionaries.
This code represents the merging of two dictionaries using a copy function and for loop:
CODE:
# concerning the first dictionary of key-value pairs
dict1 = { 'susan' : 27,'sam' : 22, 'jean' : 29, 'jake' : 30}
# taking into account the second dictionary of key-value pairs
dict2 = { 'ash' : 19, 'may' : 26,'george' : 30 }
#Before merging the two dictionary
print("Dictionary Nunmber 1 is : ", dict1) # printing dict1
print("Dictionary Number 2 is : ", dict2) # printing dict2
#using copy function to copy the dictionary
dict3 = dict1.copy()
for key, value in dict2.items():
dict3[key] = value
print("after the two Dictionary's were combined ")
# printing the merged dictionary
print(dict3)
OUTPUT:
Dictionary Nunmber 1 is : {'susan': 27, 'sam': 22, 'jean': 29, 'jake': 30}
Dictionary Number 2 is : {'ash': 19, 'may': 26, 'george': 30}
after the two Dictionary's were combined
{'susan': 27, 'sam': 22, 'jean': 29, 'jake': 30, 'ash': 19, 'may': 26, 'george': 30}
In the Python Dictionary, the content of the second dictionary is added to the current dictionary using the update() method. We can avoid making a 3rd dictionary to hold the first dictionary component and then update the 2nd dictionary element to the 2nd dictionary element by using the update() method. Consider a Python program that merges two dictionaries without creating a third dictionary.
CODE:
dict1 = {'Actress ' : 'samantha', 'Cricketer' : 'Dhoni', 'Basketball': 'Jordan', 'Football' : 'Messi'}
# Defines the d2 dictionary
dict2 = {'Tennis ' : 'Stepanos', 'Stadium ' : 'quba', 'Basketball' : 'US', 'Actress' : 'rashmika'}
dict1.update(dict2)
#Concatinating two dictionaries
print(dict1)
OUTPUT:
{'Actress ': 'samantha', 'Cricketer': 'Dhoni', 'Basketball': 'US', 'Football': 'Messi', 'Tennis ': 'Stepanos', 'Stadium ': 'quba', 'Actress': 'rashmika'}
Use the Function to combine two dictionaries in Python. Let's look at a Python program that merges the supplied dictionaries that use the update() method throughout the function.
CODE:
def merging_twoDict(d1, d2):
return (d1.update(d2))
d1 = {'UK' : 'USA', 'Germany' : 'England', 'AUS' : 'UAE' }
d2 = {'India' : 'SA', 'RUS' : 'China', 'Japan' : 'Greenland'}
merging_twoDict(d1, d2)
print("Merged Dictionaries is : ")
print(d1)
OUTPUT:
Merged Dictionaries is :
{'UK': 'USA', 'Germany': 'England', 'AUS': 'UAE', 'India': 'SA', 'RUS': 'China', 'Japan': 'Greenland'}