DEV Community

loading...
Cover image for #Day7 - Dictionaries in Python

#Day7 - Dictionaries in Python

Rahul Banerjee
Comp Eng Student @uoft | My opinions are my own Add me on LinkedIn: https://www.linkedin.com/in/rahulbanerjee2699/
Originally published at realpythonproject.com ・3 min read

Today we will be talking about some advanced concepts of Dictionaries in Python. Dictionaries are essentially a Hashmap in Python and have a lookup time of O(1)

image.png

The dictionary dict_10 has 10 elements and the dictionary has dict_100000 has 100000 elements. Both the if statements check if an element is a member of the dictionary but irrespective of the size of the dictionary size, the time taken is almost the same (It's in nanoseconds)

Keys

An object can be a key of a dictionary if and only if it's hashable. As I discussed in my previous article (Day 5), only String, Boolean, Integer, Float, Tuples, and frozen Sets are hashable while lists, sets, and dictionaries are not hashable.

Accessing an element

image.png
Although you can use [ ] operator, it is safer to use the get() function. If the key is not present in the dictionary, it returns a None instead of raising a Key Error. Additionally, you can also pass the default value. In the last print statement, a default empty string is returned if the key is not found.

Inserting a key-value pair where the value is a list or dictionary

image.png

In line 2, since key2 doesn't exist the second line would give an error. To avoid the error, we can use the setdefault() function. If the key doesn't exist, it creates a key-value pair with the default value specified. This can also be used when we have a dictionary as a value.

Merging Dictionaries

The update method merges two dictionaries.

image.png

In the above case, the key-value pairs of dict2 are added to dict1. If there are any common keys present, the value is updated. In the above case, key1 is in both dictionaries. As a result of the update, the value of key1 is changed to Value 3
Below is the output of the print statement

{'key1': 'Value 3', 'key2': 'Value 2'}
Enter fullscreen mode Exit fullscreen mode

image.png

In the above case, the value of key1 in dict2 changes to Value 1. Below is the output of the statement

{'key1': 'Value 1', 'key2': 'Value 2'}
Enter fullscreen mode Exit fullscreen mode

Copying Dictionaries

image.png

Copying using the assignment operator creates a shallow copy, i.e it actually creates and assigns a reference to the dictionary. Any changes made in dict2 are also reflected in dict1. We can use the id() function to check the memory address of the dictionaries, both of them have the same address. As a result, any changes made in dict1 or dict2 are reflected in both dictionaries.

image.png

In the above case, we are creating a deep copy, i.e the values are copied. Therefore any changes in dict2 are not shown in dict1. It is always safer to do a deepcopy to avoid unintentional updates to the original dictionary

Summary

  • A dictionary has a lookup time of O(1)
  • It is safer to use the get() method when accessing elements in a dictionary
  • Use setdefault() when you want to store a create a dictionary of lists or a dictionary of dictionaries
  • Use deepcopy to copy dictionary to avoid unintentional updates to the original dictionary

Discussion (0)

Forem Open with the Forem app