LEARN About

Saurabh Singh
6 min readSep 23, 2021

Dictionaries in python 🐍

Hi everyone, in this article, we will talk about Python dictionaries. You can read about Python lists and tuples.

Description --
A dictionary is used to store data in the form of key: value pairs. Moreover, dictionaries in leteast Python version are changeable, which means that we can add, update or delete items after the creation of a dictionary. Be careful that the values of a dictionary are mutable, not the keys. If we try to change the value of a key we will get an error. Another feature of dictionaries is that they don’t allow duplicate values, so we cannot have more than one element with the same key. From the Python perspective, a dictionary is defined as an object of class dict. You can check it using the function type().

Create a dictionary
We can create a dictionary with various ways, but the most common of them is by using curly brackets having keys and values separated by commas, as following:
my_dict = {
"keystr" : valueint,
keyint : “valuestr”,
 ...
"keystr" : valueint
}

Let’s see some examples in the code below:

Access in dictionary items

We know well about lists and tuples, we read that we can have access to their elements using the index of each element. In dictionaries, we do not have index, so we can have access to the elements of a dictionary using their keys inside square brackets, or we can use the get(key) method like below:

Moreover, we can get all the values of a dictionary in a list using the method values(). Be careful that if we change the value of an element in the dictionary, that change will be reflected in the list as well.

Let’s see the code below:

Similarly, we can get all the keys of a dictionary in a list using the keys() method. Be careful, that if we add or remove one or more keys, it will be reflected in the list as well.

Let’s see an example below:

Also, we can get all the key: value pairs of a dictionary in a list of tuples using the items() method. As we saw above, all the changes in the dictionary will be reflected in that list.

Let’s see an example below:

if we want to check the existence of a key in a dictionary we can use the in keyword as follows:

Add elements to dictionary
As we saw above in the examples, we can add new elements in a dictionary using the following syntax:
my_dict['new_key'] = new_value

Let’s see an example below:

Update dictionary elements
We can update the value of an element using the update({“key” : value”}) method or we can use the syntax below:
my_dict['existing_key'] = new_value

Let’s see an example below:

Delete dictionary elements
To delete a dictionary element we can use various methods like pop(“key”) which removes the element with the particular key or the popitem() method which removes the last inserted element. Also, we can use the del keyword with a key to remove the element with that key. With the del keyword, we can delete the whole dictionary as well. Last but not least, we can empty the dictionary using the clear() method.

Let’s see all of them in the example below:

Loops in dictionaries

We can loop through a dictionary using a for loop. However, we can have different results as we can loop through in keys of the dictionary or in the values of the dictionary or even both of them. So, using the value() method we can loop through the values of a dictionary. The syntax is the following:
for value in my_dict.values():
do something with value

Similarly, we can use keys() method to loop through the keys of a dictionary using the following syntax:
for key in my_dict.keys():
do something with key

Moreover, we can use items() method to loop through both keys and values using the following syntax:
for key, value in my_dict.items():
do something with key and value

Last, we can use the following syntax to loop through the values of a dictionary:
for key in my_dict:
do something with my_dict[key]

Let’s see all with the following example:

Dict Comprehensions

We can create a dictionary using a more efficient and elegant way using the dict comprehension.

Let’s see the example below:

Copy dictionaries

As we mentioned in the article about lists, we cannot use the syntax
dict2 == dict1

This is because we don’t create a new copy of our dict1 but instead we create another reference for the dict1. As a result, whenever we change a value in dict1, will affect and the dict2.

Let’s visualize it with a simple example.

To really create a copy of a dictionary we have a couple of options. One of them is using the copy() method. This method creates a copy of the initial dictionary.

Let’s see the example below:

As we can see, changes in our initial dictionary don’t affect the second dictionary. Another way to create a copy of our list is by using the constructor dict.

Let’s see the example below:

Nested Dictionaries

A dictionary can contain any data type like lists, tuples even dictionaries. Nested Dictionary is a dictionary that contains other dictionaries.

Let’s see an example below:

As we can see above, we can have access to the inner dictionary using the keys of dictionaries. Be careful, if you want to create a copy of a nested dictionary the copy() method will not work.

Let’s see an example below:

To create a copy of a nested dictionary you have to use the deepcopy() method as following:

Methods and functions with dictionaries

Above we used a bunch of methods to add, update, remove or loop through dictionaries. However, there are some methods we didn’t refer so far. One of them is the fromkeys() method which returns a dictionary with the specified keys and the specified value.

Let’s see an example below:

Another method we didn’t discuss is the setdefault() method, which returns the value of a key, if the key exists, otherwise inserts the key, with the specified value.

Last, we have the len() function, which returns the number of items of a dictionary.

Conclusion

In this article, we analyzed the dictionary data type of Python. Dictionaries are very useful if we want to store data in a key: value form. We will discuss more data types in Python in the future. Until then, have fun, read, learn and of course, keep coding. Thanks for reading 🙏.

--

--