Python Code

How to Create a Dictionary in Python: 4 Code Tweaks ( Conversion, Merging )

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.

Method 1

Existing data in the dictionary.

disc= {"name":"John","age":26} # existing dictionary

Print 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 {}.

Method 2

Create an empty dictionary

dis = {} #empty dictionary

Take 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] = value2

Print 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'}

Full Explanation

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).

Other Tweaks and Examples

1. How to print  a single key values from the dictionary?

You have an existing dictionary.

dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} #dictionary

You 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

value3

2. How to print all key values from the dictionary?

We will take the same dictionary.

dis = {'key1':'value1','key2':'value2','key3':'value3','key4':'value4'} #dictionary

Print 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
value4

Full 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

Create a List

3. How to convert the dictionary to list in Python?

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'} #dictionary

Make an empty list for both the keys and values.

keysList = []  #empty key list
valuesList = [] #empty values list

Append 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 valuesList

Print 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']

Full Explanation

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.

4. How to Merge two Python Dictionaries in a Single Expression?

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 dictionary

Create 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 call

Full 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 dictionary

Output

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 'key5': 'value5', 'key6': 'value7'}

Full Explanation

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.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner