#Day7 — Dictionaries in Python — Python/Data Science Tutorials and Projects

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)

Time for membershit test

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

Dictionary Get

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

Dictionary setdefault

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.

Dictionary merging

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'}
Dictionary Merging

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'}

Copying Dictionaries

Dictionary Copying

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.

Dictionary deep copy

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 create a dictionary of lists or a dictionary of dictionaries
  • Use deepcopy to copy dictionary to avoid unintentional updates to the original dictionary

Originally published at https://www.realpythonproject.com on March 28, 2021.

Programming Tips

Everything from SQL to Python

Rahul Banerjee 👨‍💻

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Programming Tips

Content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Rahul Banerjee 👨‍💻

Written by

Articles related to Python, Data Science, APIs |SDE Intern @Amazon | Comp Eng Student @uoft

Programming Tips

Content related to programming: tips, code-snippets, concepts. Follow along https://linktr.ee/rahul2699

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store