Dictionaries in Python

Join over 2 million students who advanced their careers with 365 Data Science. Learn from instructors who have worked at Meta, Spotify, Google, IKEA, Netflix, and Coca-Cola and master Python, SQL, Excel, machine learning, data analysis, AI fundamentals, and more.

Start for Free
Martin Ganchev Martin Ganchev 21 Apr 2023 6 min read

If you know what lists and tuples are, it will be easier to understand what dictionaries in Python are about. They simply represent another way of storing data.

How to Declare Dictionaries in Python

Each value is associated with a certain key. More precisely, a key and its respective value form a key-value pair.

I believe in learning by doing, so how about we give an example? Let’s create a dictionary called dict with 4 keys. We’ll assign names of different animals to each one of them.

Assigning names of different animals, dictionaries in python

Important: Be aware that neither parentheses nor brackets will work in this case – we need curly braces.

Getting Access to Elements from Dictionaries in Python

After a certain dictionary has been created, a value can be accessed by its key, instead of its index! ‘k1’ can be used for cat,

k1 for cat, dictionaries in python

while ‘k3’ for mouse!

k3 for mouse, dictionaries in python

Adding Values in Dictionaries

Similarly, as we can do with lists, we can add a new value to the dictionary in the following way. k5 for parrot, dictionaries in python

The value we’ll assign to ‘k5’ is parrot.

Swapping k2 from dog to squirrel, dictionaries in python

Replacing Values in A Dictionary

Replacing a value follows the same syntax; we can just let the new variable correspond to an existing key.

Swapping k2 from dog to squirrel, dictionaries in python

From that moment on, attached to the second key, we will see ‘squirrel’, not ‘dog’.

Lists in Dictionaries

If you are wondering if a list could be able to take part in a key-value pair, then you are right. Let’s turn to another example. We can create the dictionary dep_workers where only Peter works in department 1, but three people work in department 2 – Jennifer, Michael, and Tommy.

dep 1 peter dep 2 Jennifer, Michael, and Tommy, dictionaries in python

Therefore, our first element is a string, while the second one - a list.

Another Way to Fill in a Dictionary

We can create a new variable and use empty curly braces to indicate it’ll be a dictionary.

We will skip placing any keys or values within the braces.

Team = {}

Instead, we can assign the keys and the values one by one later on.

Creating a new dictionary, dictionaries in python

In order to see if we have done our work properly, we may ask for the ‘Center’ and expect to see “Hector”.

Print team 'center' equals 'hector', dictionaries in python

The .get() method

Let me introduce you to an interesting Python feature. The .get() method is similar to the normal way we access values in a dictionary. For instance, we can use it to get the name of the ‘Small Forward’.

Print team 'small forward' equals 'Sean', dictionaries in python

The difference between standard indexing and the .get() method comes when we ask for the name of a non-existent key. You can look at the picture below to see what happens when we ask for the name of the ‘Coach’.

Error - print team 'coach' equals none, dictionaries in python

The .get() method will not display an error. “None” is the default value Python returns in cases where an object does not actually exist within a given dictionary.

What are Dictionaries in Python Useful for?

Now, you can imagine that dictionaries in Python could do a great job, sometimes. For example, when using companies’ names as keys and their prices on the market as values. Working with dictionaries is similar to working with lists. However, there is another way you can gain access to a certain element in a dictionary. This is done by using the get() method.

And if you’re wondering how you can possibly make the machine do the work, while you write just a few lines of code, feel free to embark on the journey of learning how to use loops in Python!

***

If you’re enthusiastic about boosting your Python knowledge, check out our Introduction to Python course.

Or, if you wonder where you can find free Python textbooks and other useful learning resources, check out our Guide on Learning Python Programming.  

Martin Ganchev

Martin Ganchev

Instructor at 365 Data Science

Martin holds an MSc degree in Economic and Social Sciences from Bocconi University. His diverse academic and research experience combined with his friendly and explanatory approach to teaching have made him one of the most beloved instructors on our team. Some of the courses he has authored include: SQL, SQL + Tableau, SQL+Tableau+Python, Introduction to Python, Introduction to Jupyter, to name a few.

Top