Unlocking Python’s Hidden Superpower: Dictionaries (Part 1 of the Ultimate Beginner Series)


If you’re still using lists for everything in Python, you’re missing out on its most underrated superpower — the dictionary.
In this blog we will understand some basic features and functionalities of dictionaries step by step.
Before we continue I would like to advise you to practice along with learning. Even the best tutorials won’t be able to teach you anything if you do not practice it yourselves. I would recommend you to use the split screen feature on your laptop/computer (if available) to simultaneously open this blog and Python so that you can practice concepts as you are learning them.
Prerequisites:
There are some things that you should already know to understand the concepts taught in this blog. They are:
List Manipulation and Iteration
Tuple Manipulation and Iteration
String Manipulation and Iteration
Traversing values stored in a list, string and tuple through it’s index value
Understanding Dictionaries
Let’s Start
First we will try to understand what is a dictionary and how is it used. Instead of memorizing fancy definitions we will first try to understand what features does dictionary offers and then we will eventually derive the definition from them.
The first feature of dictionary is:
It is an unordered collection of data
We will try to understand what I mean by saying that dictionaries are “unordered collection of data”.
By unordered collection what I mean is that values stored in a dictionary are not indexed like values stored in list, tuples or strings. This means that in dictionaries values are not stored in a particular order and they cannot be accessed using index values like in tuple or list because they are not indexed.
In list we can access values using index as shown:
a = [2, 3, 9]
print(a[1])
Output will be:
3
As you can see that we were able to access a value stored in a list using the index of that value. We can also access values stored in a tuple or string with it’s index value as shown:
a = ("Hello", "Hi", "Welcome")
b = "Welcome to Python"
print(a[2])
print(b[4])
Output will be:
Welcome
o
You have already studied these concepts.
Now we will try to understand why we cannot fetch data stored inside a dictionary using index.
To understand this we must first try to understand how data is stored inside a dictionary
How data is stored in a dictionary?
When we talk about list then values are stored inside square brackets and they are separated by commas. When we talk about tuple we can say that values are stored inside parenthesis and they are separated by commas. When we talk about strings we can say that values are stored inside double inverted commas.
Now let’s talk about dictionary
In a dictionary values are stored in the form of pairs inside curly brackets. These pairs are known as key value pairs. Each pair is separated by a comma.
Pairs are written in the following manner:
Key : Value
To understand how data is stored in a dictionary we will try to understand the example given below.
d = {2 : "Hello"}
In the example mentioned above you can see a key value pair stored in a dictionary named “d”. The key here is 2 and the value here is “Hello”. We can also say that in this case the key is an integer while the value is a string.
We can also store multiple key value pairs inside a dictionary as shown:
d = {1 : "Hi", 2 : "Hello", 3 : "Bye"}
As you can see that we have written 3 key value pairs which are separated by commas inside a dictionary. In each key value pair we have used an integer as a key and a string as value.
Several doubts must be coming in your mind. We will discuss them one by one in this blog. When I was studying this topic the doubt that I had was that
Can we use any data type as key or value?
This is exactly what we will be discussing next. Before continuing it is essential to summarize what we have studied till now.
Important Point No.1:
Important Point No.2:
Important Point No.3:
Let’s Continue
We can only use a immutable data type as a key. Values can be either of immutable or mutable data type.
We know that lists are mutable data types so they cannot be used as a key. Let’s see what happens when we create a dictionary and use a list as a key.
dict1 = {["Hello"] : "Hi"}
The output of this code will be:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python313\python dictionary.py", line 1, in <module>
dict1 = {["Hello"] : "Hi"}
TypeError: unhashable type: 'list'
As you can see an error named “TypeError” is being generated by Python. The last line of the error is:
TypeError: unhashable type: ‘list’
This error means that we used a list as a key in dictionary which is not acceptable because a key can only be of an immutable type and list is a mutable data type.
Now let’s use list as a value to check whether Python generates an error or not.
dict1 = {"Hi" : ["Hello"]}
print(dict1)
In this example we have used list as a value and then displayed that dictionary using print() function. The output of the code is:
{'Hi': ['Hello']}
As you can see that the dictionary got displayed without any error. This confirms that mutable data types can be used as values in a dictionary but not as a key.
Important Point No.4:
If we use a number or string as a key then no error will be generated because they are immutable data types. But what will happen if we use tuple as a key?
You might be thinking that since tuple is an immutable data type so we can use it as a key in dictionary. But this is not always true. Let’s analyze why.
We will analyze two examples to learn when can tuple be used as a key in dictionary.
dict1 = {("Hello", "Hi") : 3}
print(dict1)
Let’s name this code as code 1.
Output of this code is:
{('Hello', 'Hi'): 3}
Second Example:
dict1 = {("Hello", [3]) : 3}
print(dict1)
Let’s name this code as code 2.
Output of this code is:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python313\python dictionary.py", line 1, in <module>
dict1 = {("Hello", [3]) : 3}
TypeError: unhashable type: 'list'
Why code 2 generated an error but code 1 didn’t even when in both the cases we used tuple (an immutable data type) as key?
The answer to this question is that tuple can only be used as a key when all the values stored in it are of immutable data type.
In code 1 tuple had two values and both of them are strings which means both of them are of immutable data type so the dictionary was displayed without any error.
In code 2 tuple had two values. One of them is a string which is an immutable data type and the other one is a list which is a mutable data type. Since in this case tuple had a value which is of mutable data type so an error was generated and the dictionary was not displayed. The error generated is:
TypeError: unhashable type: ‘list’
This is the same error generated previously when we used a list as key. In this case we didn’t used a list as a key but we used a tuple which had a list as one of it’s value which is why this error was generated again.
Important Point No.5:
Till now we have discussed what is a dictionary and how values can be stored inside it. Now we will discuss how can we access values and keys of a dictionary. You might feel overwhelmed so I suggest that you should practice the concepts that we have discussed till now by creating dictionaries on your own before continuing.
If you have understood and practiced what we have discussed till now then let’s continue
How to display/obtain values stored in a dictionary?
Let’s assume I have created a dictionary where I have used names of some students as keys and there respective marks as the values of those keys as shown:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
The marks of John, Arjun and Aakash is 80, 90 and 50 respectively. Let’s suppose I want to display marks of a particular student then how will I do it?
To get the value of a key we have to use the following statement:
<name of dictionary>[<key>]
We do not have to use greater than and less than equal symbols while writing code. This is a way to write syntax.
If we want to display marks of the student named - Aakash then we have to write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
print(dict1["Aakash"])
Output of this code is:
50
To display the marks of the student Aakash we have used print() function and inside the print() function we have written the expression required to get the value. The expression:
dict1[“Aakash”]
is used to obtain the value of the key “Aakash” in the dictionary named dict1.
Thus we can conclude that to obtain the value of any key in a dictionary we must write the dictionary name first and inside square brackets we will write the key whose value we want to get.
This becomes our 6th important point.
Important Point No.6:
If we want to display a value then we must use print() function as we did in the previous example. If we do not use print() function and simply write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
dict1["Aakash"]
then no output will be displayed. To display output using print() function is necessary.
If we want to display entire dictionary then we will simply write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
print(dict1)
Output will be:
{'John': 80, 'Arjun': 90, 'Aakash': 50}
if we want to display the value of key “John” we will simply write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
print(dict1["John"])
Output will be:
80
We have discussed how to display value of a particular key of a dictionary. I suggest that you should try to display values of several keys of a dictionary to fully internalize this concept.
Sometimes we do not need to display the value of a key but we need to store that value somewhere so that it can be used later in the program. To do so we will write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
a = dict1["John"]
The value of the key “John” of the dictionary dict1 has been assigned to the variable “a” as it’s value. So now whenever we have to use the value of the key “John” to perform some operation later in the program we can use the variable “a”. Storing some values inside variables can be helpful in situations where we have to compare them or modify the value of a key of a dictionary while preserving the original one.
It might be slightly difficult for you to understand what I just said. But don’t worry I will cover the entire concept of dictionaries in several parts (this is part 1) so that by the end everything will be crystal clear to you.
Now we will learn how to obtain or display keys of a dictionary
How to display/obtain keys stored in a dictionary?
To display each and every key of a dictionary we can use the for loop. How does for loop works with dictionaries and how can we use it? To understand this we will analyze an example shown below:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
for i in dict1:
print(i)
Output of this code will be:
John
Arjun
Aakash
Now we will try to understand what has happened and why this output was produced.
If you look carefully at the code which we have written above then you can clearly see that we have used for loop to iterate dictionary. Whenever we use for loop with a dictionary then all the keys get assigned to the variable “i” one by one.
So in the first iteration the first key which is “John” got assigned to the variable “i”. When I printed this variable then as a result John was displayed in the output.
In the second iteration the key “Arjun” was assigned to the variable i. When I displayed the variable i then as a result Arjun was displayed.
In the third iteration the key “Aakash” was assigned to the variable i. When I displayed the variable i using print() function then Aakash was displayed in the output. To understand this you should have some knowledge of for loop.
To understand this concept even more deeply I will discuss one more example.
If we want to display output in the following manner:
first key : value of first key
second key : value of second key
third key : value of third key
then we will write the following code:
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
for i in dict1:
print(i, ":", dict1[i])
The output will be:
John : 80
Arjun : 90
Aakash : 50
Now we will try to understand how this output was displayed.
As we discussed that keys present in dictionary get assigned to the variable i one by one when we use for loop.
So in the first iteration the key “John” got assigned to variable i which means that now for this iteration value of variable i is “John”.
After this assignment print() function was executed which is:
print(i, “:”, dict1[i])
Since we displayed i and dict1[i] so both the key and value of that key was displayed. In the first iteration when the value of variable i is “John”, the key John a colon and the value of this key was displayed.
Then comes the second iteration. Now the variable i has value “Arjun”. So now when print() function gets executed the key “Arjun” along with it’s value gets displayed.
The same happened with third iteration.
Important Point No.7:
Built in methods to get keys and values of a dictionary
Python offers two built in methods using which we can access keys or values of a dictionary. By built in method what I mean is that these methods are already defined in Python like pre defined functions.
The syntax for built in method used to access keys of a dictionary is:
<name of dictionary>.keys()
The syntax for built in method used to access values of a dictionary is:
<name of dictionary>.values()
Note: You don’t have to use greater than or less than symbols when writing the name of dictionary. This is just a way to write syntax.
If we want to display list and values using these methods then we have to use print() function. How these methods work and how we can use them is depicted in the code written below.
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
print(dict1.values())
print(dict1.keys())
Output of this code will be:
dict_values([80, 90, 50])
dict_keys(['John', 'Arjun', 'Aakash'])
As you can see we have used the expression dict1.values() with print() function and values present in our dictionary was displayed. We used the expression dict1.keys() with print() function as a result keys present in our dictionary got displayed.
I used dict1 with .keys() and .values() because dict1 is the name of our dictionary. If name of our dictionary was something else let’s say d then I would have written d.keys() and d.values().
Important Point No.8:
These two methods are beneficial if we want to store keys and values present in our dictionary separately. We can use use these two methods with list() or tuple() method to convert them into list or a tuple respectively and store them. In this way we can store the keys and values present in our dictionaries separately. Read the following code to understand what I am talking about.
dict1 = {"John" : 80, "Arjun" : 90, "Aakash" : 50}
a = list(dict1.values())
b = list(dict1.keys())
print(a)
print(b)
Output of this code is:
[80, 90, 50]
['John', 'Arjun', 'Aakash']
Now let’s analyze what has happened. I have used list() method with dict1.values() as a result all the values present in the dictionary got compiled in a list and that list was stored in variable a. I also used list() method with dict1.keys() as a result all the keys present in the dictionary got compiled in a list and that list was stored in variable b.
When I displayed a and b using print() function then all values compiled into a list and all keys compiled into a list was displayed respectively.
Now the question is what advantage does this approach of compiling all values and keys into a separate lists gives us?
In some scenarios this approach can benefit us greatly. Let’s suppose I am writing a Python program in which I am storing Roll number and Names of some students in a dictionary. I am storing names as keys and roll numbers as values. If I want names of all the students and roll numbers of all the stored separately then I can use the approach mentioned above to compile names and roll numbers into separate lists. In this manner we also have a dictionary in which names and roll numbers are stored as key value pairs so that we can know which roll number belongs to which student and we also have a separate collection of names of students and there roll numbers.
Summary and Mind Map
Here I will summarize all the important points so that you can read and revise them for future reference.
Important Point No.1:
Data is stored in the form of pairs inside a dictionary. These pairs are known as key value pairs.
Important Point No.2:
Key value pairs are stored inside curly brackets to create a dictionary.
Important Point No.3:
Multiple key value pairs stored inside a dictionary are separated by commas.
Important Point No.4:
Only immutable data types can be used as keys. Values can be either of immutable or mutable data type.
Important Point No.5:
A tuple can only be used as a key if all it’s values are of immutable data type.
Important Point No.6:
To obtain the value of any key in a dictionary we must write the dictionary name first and inside square brackets we will write the key whose value we want to get.
Important Point No.7:
for loop can be used along with dictionaries to access keys of a dictionary.
Important Point No.8:
Built in methods like keys() and values() are provided by Python using which we can access keys and values present in our dictionary.
I have also prepared a mind map which has summarized all the important concepts taught in this blog. You can take a screenshot of it for future reference.
This image should not be used for commercial purpose without my permission. It has only been shared for learning purpose.
I have tried my level best to teach basics of dictionaries in this blog. I hope that it was helpful for you. If you have any doubt related to the content taught in this blog you can ask it in comments section. I will reply as soon as possible.
A Short Introduction
Hi I am Satyarth Shree, a 17 years old student who will be soon joining a college to pursue BTech in Robotics and Automation. I document my robotics learning journey publicly using Medium and Hashnode. Hashnode is my primary blogging platform while Medium is my secondary blogging platform. If you wish to read my Medium blogs then click here.
My most prominent blog is related to a Habit Tracker tool which I built using Python. Although I lost the original tool I have documented what I have learned while building this project. You can click here to read it.
Let’s Connect
My Medium Blogs: https://medium.com/@satyarthshree45
My Github: https://github.com/Satyarth-Shree
My LinkedIn: https://www.linkedin.com/in/satyarth-shree-761357374/
My X: https://x.com/BuildUnderdog
My Portfolio: https://satyarth-shree.github.io/My-Portfolio/
My CodeWars: https://www.codewars.com/users/Satyarth-Shree
To professionally contact me for internship, job or similar opportunities message me through this form: https://forms.gle/cQ4whViqLRKNgSL68
Thank You
Satyarth Shree
Subscribe to my newsletter
Read articles from Satyarth Shree directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Satyarth Shree
Satyarth Shree
I’m Satyarth Shree — a Robotics & Automation engineering student learning in public. I write beginner-focused tutorials on Python, Git, C programming, and system logic while building a deeper understanding of robotics, embedded systems, and automation technologies. My long-term goal is to become a well-rounded robotics engineer by mastering both the software and system layers — from microcontroller code to full-stack control panels. This blog is my personal lab notebook — documenting each step, mistake, and breakthrough as I turn learning into clarity and clarity into leverage. Hashnode is my primary blogging platform while Medium is my secondary blogging platform. My medium profile link is:https://medium.com/@satyarthshree45