# An Overview of Dictionaries in Python

WHY USE DICTIONARIES? HOW TO USE DICTIONARIES? WHAT ARE THE BENEFITS OF USING DICTIONARIES?

All these above questions will be answered here.

Dictionaries play an important role in Data World.
Imagine ….
You are working in a school as a counselor, and want to keep track of the student in each class.
You can put the strength of students in a list, here I am taking a group of 5 class dataset.

student=[52,35,65,41,53]

And to keep a track of which class contains a certain no. of students, we will create another list with the class name.

classes =[’10A’,’12B’,’10B’,’11A’,’10C’]

Now suppose that you want to find out how many students are there in the “11A” class.
Firstly you have to figure out where in the list is “11A”; so that you can use this position to get the correct student number.

So, we will use index(), to get the index of “11A” in class, like this:

ind_11A = classes.index(’11A’)

Now we can use this index to subset the student list, to get the students corresponding to ’11A’.

ind_11A

Out: 3

student[ind_11A]

Out: 41

Yeah, so here we built 2 lists and used the index to connect corresponding elements in both the lists.
And, it worked!
But, it’s a pretty terrible approach, in other words, it’s not convenient.

Wouldn’t it be easier if we had a way to connect each class to its student, without using any index??

This is where DICTIONARY comes into the picture.

Let us convert the class data to a dictionary….

It starts with “{” (curly braces), and inside curly braces, you have a bunch of what we call key: value pairs.
Keys and values are separated by : (colon).

In our case, the keys are class names, and values are the corresponding students.

school = {’10A’ : 52, ’12B’ : 35, ’10B’ : 65, ’11A’ : 41, ’10C’ : 53}

If now you want to find out the number of students in “11A”, you can simply type school and then the string “11A” inside the
square brackets.

school[’11A’]
Out: 41

In simple words, you pass the key in square brackets and you get the corresponding value.

“THE KEY OPENS THE DOOR TO THE VALUE”: pretty poetic, isn’t it?

This approach is not only intuitive but also very efficient, because python can make the lookup of these keys very fast,
even for huge dictionaries.

A point should always be remembered while using dictionaries, that is:
The key should be unique.

SOME COOL FUNCTIONS OF DICTIONARIES:

• To add a new key to the existing dictionary –       school[’12A’] = 45
• To remove a key –          del(school[’12A’])
• To check if the key is in dictionary –        “10A” in school
• To copy a dictionary into another dictionary –       school2 = school.copy()
• To create dictionaries having dictionaries –       Dict = { ‘Dict1’: {1: ‘G’, 2: ‘F’, 3: ‘G’}, ‘Dict2’: {‘Name’: ‘Geeks’, 1: [1, 2]} }
• To get a certain element from just one dictionary –      Dict[‘Dict1’][1]

So, these were some basic functions related to dictionaries. I hope now you are well versed in the benefits of using dictionaries.