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.
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,
while ‘k3’ for mouse!
Adding Values in Dictionaries
Similarly, as we can do with lists, we can add a new value to the dictionary in the following way.
The value we’ll assign to ‘k5’ is parrot.
Replacing Values in A Dictionary
Replacing a value follows the same syntax; we can just let the new variable correspond to an existing key.
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.
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.
In order to see if we have done our work properly, we may ask for the ‘Center’ and expect to see “Hector”.
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’.
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’.
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.