Creation, Addition, Removal and Modification of Dictionary in Python

In this post, we will tell you everything about dictionary in python, viz. how to create dictionary, how to add, remove and modify elements in dictionary, etc.

Why do we need dictionary?

Suppose we have to store marks secured by various students of a university. In order to meet our requirement, we need a data structure which can store two values at a time(name of student and marks scored by them)which can be mapped to each other and both of them have different data type. Thus arises the need of dictionary.

How to create a dictionary?

A dictionary in Python is a data structure which can store the values pairwise where both the elements have different data type. Each entry in a dictionary has a key and a key-value mapped to it. A key must be unique and of immutable data type(which can’t be changed once created, like string or integer) while there is no such restriction on the key-value. They can be repeatable and of any data type.



An empty dictionary can be declared as follows:

#creating an empty dictionary
marks_dict={}

How to add elements in a dictionary?

We take the key and assign it to its key-value using the name of dictionary. The sample code is as follows:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#printing elements of dictionary
print(marks_dict)

The output of the code written above is:

{'Shreyanshi': 93, 'Pragya': 68, 'Srishti': 85, 'Lakshmi': 72, 'Jyoti': 89}

How to remove elements from a dictionary?

Suppose we have to delete any entry from our dictionary. We can use the “del” in-built function in a dictionary. The syntax of deletion of any element is:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#deleting elements from dictionary
del(marks_dict["Pragya"])
del(marks_dict["Srishti"])

#printing elements of dictionary
print(marks_dict)

The output produced of the code above will be:

{'Shreyanshi': 93, 'Lakshmi': 72, 'Jyoti': 89}

How to modify elements of a dictionary?

Sometimes we need to change the key-values which we inserted earlier. In this case we only need to update the key-value of that particular key. The sample code is as follows:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#modifying elements of a dictionary
marks_dict["Srishti"]=81

#printing elements of dictionary
print(marks_dict)

The corresponding output is:

{'Shreyanshi': 93, 'Pragya': 68, 'Srishti': 81, 'Lakshmi': 72, 'Jyoti': 89}

How to use keys and key-values in a dictionary?

To fetch all the keys or the key-values separately, stored in a dictionary we can use “key” or “values” function with the name of dictionary. It returns a list of keys or key-values as per requirement. For example:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#get keys and key-values
print(marks_dict.keys())
print(marks_dict.values())

The output of the above sample code is:

dict_keys(['Shreyanshi', 'Pragya', 'Srishti', 'Lakshmi', 'Jyoti'])
dict_values([93, 68, 85, 72, 89])

We can use the following piece of code to check if a key is already present in the dictionary or not:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#to check if a key is present in a dictionary
if "Jennie" in marks_dict:
    print("Yes!! It exixts.")
else:
    print("Record not found...........")

The result of this code is:

Record not found...........

To access the key-values we have two methods. The first one is by using the name of key inside square brackets with the name of dictionary and the other method is by using the “get” function demonstrated as follows:

#creating an empty dictionary
marks_dict={}

#adding elements in the dictionary
marks_dict["Shreyanshi"]=93
marks_dict["Pragya"]=68
marks_dict["Srishti"]=85
marks_dict["Lakshmi"]=72
marks_dict["Jyoti"]=89

#to retreive the key-value of a key
print(marks_dict['Jyoti'])
print(marks_dict.get('Lakshmi'))

The outcome of the code written above is:

89
72

This summarizes the most common operations we can perform on the dictionary in Python.

You can also learn,

Leave a Reply

Your email address will not be published. Required fields are marked *