Dictionaries are the unordered way of mapping and storing objects. It uses the key-value pair. It enclosed by the curly braces {}. Let’s start how to create a Dictionary in Python.
Existing data in the dictionary.
disc= {"name":"John","age":26} # existing dictionaryPrint the dictionary.
{'name': 'John', 'age': 26}Full Explanation
This is the simplest method for creating a dictionary. You have to just assign the string in this format {“name”:”John”,”age”:26} to the variable disc. When you print it will output in the same format. Just keep in mind that dictionary is enclosed by Curley braces {}.
Create an empty dictionary
dis = {} #empty dictionaryTake inputs from the user.
key1 = input("Enter the key-")
value1 = input("Enter the value-")
key2 = input("Enter the key-")
value2 = input("Enter the value-")Assign the values to the corresponding key
dis[key1] = value1
dis[key2] = value2Print the dictionary.
print(dis)Full Code
dis = {} #empty dictionary
#input from the user
key1 = input("Enter the key-")
value1 = input("Enter the value-")
key2 = input("Enter the key-")
value2 = input("Enter the value-")
#Assign the values to the key
dis[key1] = value1
dis[key2] = value2
#display the dictionary
print(dis)Output
Enter the key-Name
Enter the value-John
Enter the key-Age
Enter the value-26
{'Name': 'John', 'Age': '26'}First of all, you have to create an empty dictionary dis = {}. All the key and values will be stored inside this empty dictionary. You have to both keys and its values from the user. Then you assign the values to the keys using dis[key] = value. At last, you will print the dictionary using print(dis).
You have an existing dictionary.
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} #dictionaryYou have to print the value of the third key3
print(dis['key3'])Full code
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
print(dis['key3'])Output
value3We will take the same dictionary.
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} #dictionaryPrint all the values of the key.
for keys in dis:
   print(dis[keys])Full Code
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
for keys in dis:
    print(dis[keys])Output
value1
value2
value3
value4Full explanation
You have to iterate the dictionary for printing the values. The for statement will find all the values of the key and print(dis[keys]) will output all the values of corresponding keys.
Related Article
You have a dictionary and you want to convert it into a list. In this example, you will know how to extract keys and values from the dictionary into a separate list.
You will take the same dictionary.
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} #dictionaryMake an empty list for both the keys and values.
keysList = []  #empty key list
valuesList = [] #empty values listAppend the keys and values in the empty list.
for keys,values in dis.items():
       keysList.append(keys) # append keys in the keyList
       valuesList.append(values) # append values in the valuesListPrint the lists
print(keysList)
print(valuesList)Full Code of the above problem.
dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
keysList = []
valuesList = []
for keys,values in dis.items():
    keysList.append(keys) # append keys in the keyList
    valuesList.append(values) # append values in the valuesList
print(keysList)
print(valuesList)Output
['key1', 'key2', 'key3', 'key4']
['value1', 'value2', 'value3', 'value4']You have an existing dictionary. First of all, you have to create two empty lists keysList = [] and valuesList = [] for storing the keys and values respectively. After that, while iterating the dictionary items ( dis.items() )using the for loop and appending the keys (keysList.append(keys)) and values ( valuesList.append(values) ) inside the keysList and valuesList.
After appending you can now print the keys and values using the print() function.
Define the merging function.
#function definiton
def mergeDictionary(dis1,dis2):
   finalDis = dis1.copy() #copy dis1 to finalDis
   finalDis.update(dis2) # concate the ds1 with ds2
   return finalDis # return the final dictionaryCreate the two dictionaries.
dis1 = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
dis2= {'key5':'value5','key6':'value7'}Call the function
mergeDis = mergeDictionary(dis1,dis2) #function callFull code
dis1 = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'}
dis2= {'key5':'value5','key6':'value7'}
#function definiton
def mergeDictionary(dis1,dis2):
    finalDis = dis1.copy() #copy dis1 to finalDis
    finalDis.update(dis2) # concate the ds1 with ds2
    return finalDis # return the final dictionary
mergeDis = mergeDictionary(dis1,dis2) #function call
print(mergeDis) #print the dictionaryOutput
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value7'}You have two dictionary dis1 and dis2. In this example we created a function mergeDictionary(dis1,dis2) . It takes two dictionary parameters dis1 and dis2. Inside the function definition, you will create finalDis to copy ds1 and update dis1 with dis2. When you call the function, it will merge the two dictionaries.