Understand the Concept of Dictionary

shipra_saxena Last Updated : 24 Jan, 2024
4 min read

Objective

  • Dictionary is another composite data type provided by python
  • Understand the concept of a dictionary and the basic operations performed on it

Note: If you are more interested in learning concepts in an Audio-Visual format, We have this entire article explained in the video below. If not, you may continue reading.

Introduction

Let’s start with a scenario. Suppose, we have stored the marks of a student sam in a list.

dictionary list

Can you tell, after looking at this list how much he has scored in History or Geography? No, you can not until you have the data where you have stored which subject marks are stored at which index.

what if not dictionary

If we have this data, you can easily answer that. Since History stored at index 1, we can say that Sam scored 36 marks in History.

But there are a few problems with this approach.

  • Many a time this data is not available or hard to create.
  • Sometimes we do not want our data to have a sequence. Like in the previous example it doesn’t matter History marks are stored after Geography or before it.
  • Also, sometimes we want to access our data by names as in the example above. For instance, finding the History marks using the word ” History” not index.

All these issues can be solved using a Python data structure called Dictionary. This is how the dictionary is going to solve our problem.

dictionary data

Instead of indices, the marks are stored using names or keys in the dictionaries. Here, the subject names Geography, History, Mathematics are the keys, and the marks 45, 36,54, etc. are the values corresponding to these keys.

What is a Dictionary?

A dictionary is an unordered data structure with elements separated by a comma and stored as a key-value pair. It is enclosed within curly brackets. Also, the key-value pair is separated by a : (colon).

dictionary 2

Here is one example of. Here, the key is the name of the person and the value is a number.

Dictionary example

We can also have any other data structure as a value. Look at the example below, here we have a list of numbers as value for each key.

Accessing Elements of a Dictionary

Now let’s see how we can access elements of a dictionary. Elements are accessed by keys rather than index. If we try to access a dictionary using the index, we get an error. Because this is not elements are stored inside it.

Elements are stored using keys. So to access them we need to use the key. Suppose you want to access the details of the key Suresh use  dict2[‘Suresh] as shown in the following image.

Accessing elements

It will return the values stored using the key Suresh i.e a list of values 146 and 58.

Adding Elements to a Dictionary

Now let’s move on to see how to add elements to a dictionary. To add a single value to the dictionary we can use the name of the dictionary along with the new key enclosed in a pair of square brackets. See the image below.

Adding Elements to a Dictionary example

When you print the dictionary you can see a new key-value pair added to the dictionary.

What if we want to add multiple elements to a dictionary. We can do this using the update function. This update function takes it as an input.

update function

In our example, we are adding to key-value pair  Sunil and Disha to the existing dictionary using the update function. When we do so the two new elements get added to the dictionary.

This was all about how we can add elements to dictionaries.

Deleting Elements from a dictionary

What if you want to delete an element? Deleting an element from a dictionary is very much similar to that of a list. The only difference is instead of the index we pass the key within the square bracket that you want to delete from it.

For example, if I want to delete the details of Ramesh. We can do del dict2[‘Ramesh] as shown below.

delete

It will remove the Ramesh key-value pair from the dictionary. After performing the deletion, it will have only two elements.

End Notes

This was all about dictionaries in this article. We saw how easily we can store and access the data in dictionaries. It uses the key-value pair format to store the values and allow instant access without any overhead. We also saw how to add and remove the elements from an existing dictionary.

Some important points about dictionaries-

  • Dictionaries are mutable.
  • They are dynamic which means they can grow and shrink according to the requirement.
  • Dictionaries can be nested.

If you are looking to kick start your Data Science Journey and want every topic under one roof, your search stops here. Check out Analytics Vidhya’s Certified AI & ML BlackBelt Plus Program

If you have any queries let me know in the comment section!

Responses From Readers

Clear

Shahnawaz Sayyed
Shahnawaz Sayyed

Very good explanation for beginner's...Its so simple and crisp.. Thanks for sharing

Devil deva
Devil deva

Sir could you please explain more stuff about dictionary . Cus I want to more about this please update it

SoundofText
SoundofText

Great blog post! I really like the explanation of how dictionaries work in Python, and how they can be used to store and manipulate data. It's helpful to have a clear understanding of what a dictionary is and how it differs from other data structures, like lists. Thanks for sharing!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details