Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

Dictionary in Python | Python for Data Science | Day 4

Hello, The Data Monk family! 
We hope you would have read our last tutorial on tuples. If not, do read the article to clarify your concepts. 
Now, it’s time for us to learn about dictionary in Python.  Python’s dictionary is analogous to hash table in C++. Dictionary in Python is a data structure that comprises key, value pairs. If it goes tangential to your head, don’t fret. Read the article. It has no prerequisites, just a will to learn!

Dictionary in Python

Without any delay, let’s learn The Data Monk way! So, open your favorite IDE and follow this article to the very end. 

First off, let’s see how to create a dictionary. 

Here’s the syntax :

my_dictionary= {Key1:  “Value1”,Key2: “Value2”,Key3: “Value3”, ….. }

Let’s look at the code now:

Here’s how you can create a dictionary.

Dictionary in Python

If you enter duplicate keys values in a dictionary, the latter value will be considered and the first one will be discarded. See below-

Dictionary in Python

Even if we entered 4 key-value pairs, the length of the dictionary is still 3 because of the dropped duplicate.

Dictionary in Python

We can print the keys and values of a dictionary by simply using the keys() and values() functions respectively.

Dictionary in Python

Suppose I need to find out the jersey color for Mumbai Indians, how should I find out? Look at the image below for the solution.

Dictionary in Python

So, Mumbai Indians wear blue. What if I need to change this value to Dark Blue? 

Changed!

Let’s see all the dictionary items using items() function.

Now I realize that I forgot to mention KKR in the dictionary. So, let’s update the dictionary.

If you are trying these examples along with us, you’re doing a great job. We are nearing the end of the article.

Let’s see how we can delete an item from the dictionary.

Punjab Kings was successfully deleted from the dictionary.

Now, let’s delete the whole dictionary. We can do so by simply using del keyword.

The error message suggests that the dictionary was successfully deleted.

Keep it up, pals! Now it’s time for us to test our knowledge. Don’t forget to answer the questions below. Though we have answered them for your convenience, we highly recommend that you spare a few minutes to attempt these on your own before looking at the solutions.

We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist 
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert

Article By,
Yashika Khurana
Intern | The Data Monk

About TheDataMonkGrand Master

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)

Follow Me

Leave a reply