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.

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics managers, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

Author: TheDataMonk

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