Intermediate Dictionary
In Python, a Dictionary is an in-built data type that stores values in key, value format. It is very similar to the data structure Hash Tables. But before we delve into Dictionaries, let us understand the concept of a dictionary using a real-world example.
We all have read a book in our lifetimes, and most people read from the beginning. However, when you get interrupted for something, be it a break from reading or for something else, you might want to go back to the chapter or the page you left. Many people use bookmarks or look up the book's index to resume their reading session.
Dictionary in Python is analogous to indexes in books. They are used to directly access a value based on a defined key or a reference, much like the indexes in a phone directory. I know people barely use the phone directory, but it's a good example nonetheless.
Dictionaries are a collection of unique references to mutable values.
In Python, references stored in Dictionary are ordered, mutable, and are unique.
Python allows for defining of Dictionary using the curly brackets {}. They store the values in key: value pairs.
{'name': 'James Bond', 'age': 46}
Additionally, they can also be defined using the in-built dict() method. Below is an example:
{'name': 'James Bond', 'age': 46}
If you have followed this tutorial, we have discussed the importance of data structures. If you haven't or you might want to revise, here is the link.
As discussed earlier, Dictionaries in Python allow for directly accessible values, unlike a List, Set, or a Tuple, where the values are accessed sequentially.
Let us take an example. Suppose you have a list of individuals whose information is stored according to their Birth Months. If we were to solve this conventionally, we could do something like this.
As you can see, the above approach is not leading to a clear, maintainable codebase. Additionally, the data values are too scattered. Since the data values logically belong to one section, having them stored as a single unit would be a much better code design.
However, doing sparsely for such a small program might still work, but it won't for more extensive programs. Ask yourself this question:
How are you going to process a million records?
To answer the above question, we immediately recognize that this approach clearly won't work. Now let us compare it with Python's dictionary implementation.
In the above example, we store all relevant values in a single data structure, i.e., a dictionary called months. Furthermore, we can access any items within it using a reference. For example, we want to access the names of people born in November; we will write the following statements:
We will discuss accessing the values in the following sections.
Since the beginning of this Python tutorial, we have been using the in-built len() method to determine the length of different data types such as List, Tuples, String. The len() method also supports the Dictionary data type and returns the number of items it stores. Below is an example:
The dictionary has 4 item(s).
Dictionary in Python supports various hashable data types as keys or references. The supported data types are:
Example:
{<class '__main__.MyFavouriteClassNameInPython'>: 1, <__main__.MyFavouriteClassNameInPython object at 0x0000023550E03FD0>: 2, <function my_favourite_function at 0x000002354F11E040>: 3, 8: 5, 9.1j: 6, (1, 2, 3, 4): 7}
Dictionary and List are Non-Hashable objects and therefore are not supported.
Good question. If you have been following this Python tutorial, we discussed how values are assigned in the variables chapter. To re-iterate, a variable is a reference pointing to an object that stores the value. The variable does not hold the value itself. It is instrumental when dealing with the Dictionary data types, as it requires a fixed reference or key to produce a value.
List and Dictionary are data types whose values can be changed while maintaining the same reference. Hence, they are non-hashable and thus unsuitable candidates for references for the Dictionary data type. This information is quite helpful as most tutorials don't cover it because it seems irrelevant . However, it is crucial for deeper understanding.
It is one of the most intriguing questions. I won't go into too much detail; give a brief overview. In the previous section, we discussed the Dictionary supporting only hashable data types only.
When we set a reference or a key, its unique id gets calculated and stored. Let us take an example:
In this example, we are trying to set a key name with an associated value , James Bond.
The key name will get converted into a number much similar to like this 948193611238. Hence, whenever my_dict is accessed using the reference name, Python will calculate its hash and fetch the associated value.
Therefore, Python can support multiple hashable data types.
Python, being a high-level language, it provides a straightforward syntax to access values in a dictionary. The syntax to access a value is as follows:
<dict_name>[key]
Example:
One
Two
Three
James Bond
We have discussed accessing Dictionary values, but what about extracting all the keys in a Dictionary and its applicability. Let us take a real-world example to help understand the importance of this helpful feature.
Assume you are creating a program to store all the languages spoken in countries. Here is what the Python representation might look like:
Now suppose, you might want to find out all the countries supported. Python allows you to extract the keys in a dictionary.
dict_keys(['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy']) <class 'dict_keys'>
This valuable feature of Python's Dictionary allows for storing both the reference and the value together in a cohesive data structure that provides for logically related data to be together.
Python allows for the conversion of keys returned by the in-built keys() method using the list() or tuple() method to convert. Below is an example:
['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy'] <class 'list'> ('usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy') <class 'tuple'>
Similar to the keys() method discussed in the prior section, Python allows for extracting the Dictionary values. For that, Python provides the conveniently named method values(). Below is an example:
english
('english', 'french')
spanish
french
mandarin chinese
english
russian
spanish
japanese
italian
We have discussed extracting keys and values from a Dictionary. What if we want to check the presence of the key before we try to access its value. To help illustrate this scenario, let us solve this problem the conventional way:
Suppose we want to find whether "usa" is present in the Dictionary or not.
USA is present
As you can observe, we had to traverse through the entire country list to verify its presence, and doing so results in a larger code base and opens opportunities for bugs to seep through.
Python, a high-level programming language, provides the in operator as discussed in the operator's chapter.
Below is the same example using the in operator:
USA is present
The above example is a much neater, understandable, and more manageable codebase.
During development, you might encounter scenarios where you want to display the key and the associated value. Here is how you would loop through the Dictionary to print the same.
usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian
Let us break down the above statement for clarity.
#1:
This statement is declaring a dictionary named countries with the languages spoken in each.
#2:
This statement fetches the key from the Dictionary and stores it in a temporary variable named country.
#3:
This statement accesses the value in the Dictionary using the fetched key.
usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian
The in-built items() method returns both the associated key and its value in a tuple.
Python being a high-level language allows for having a Dictionary with a Dictionary. Here is an example:
3979576
212869
1330612
Having nested Dictionaries help with accessing data managed by categories.
In the above example, we categorized cities under their respective states and then access their population details.
The nested Dictionary helps store and access categorized data.
The in-built clear() method clears the entire contents of the Dictionary.
{1: 'One', 2: 'Two', 3: 'Three'}
{}
We have discussed this earlier as well; there are referenced-based and value-based data types. In Python, Dictionaries are referenced based. Hence, simply assigning the Dictionary to a new variable won't create a new Dictionary; it will refer to the same Dictionary. Below is an example:
{1: 'Four', 2: 'Two', 3: 'Three'}
As you can observe from the output, my_dict is changed, although we did not directly change it. It is because the variable x is now referring to my_dict. Hence, any change made in any variable, either x or my_dict, will reflect when accessing the Dictionary using any reference.
To create a 'deep' copy of the Dictionary, Python provides the in-built copy() method.
{1: 'One', 2: 'Two', 3: 'Three'}
{1: 'Four', 2: 'Two', 3: 'Three'}
This in-built fromkeys() method will create a dictionary with a specified key and value. This method is helpful when you might want a Dictionary having the same default values for initialization.
{'name': '', 'age': '', 'address': ''}
The values argument in fromkeys() method is optional and defaults to None.
This in-built get() method in Python will return the value from the Dictionary. If the key is absent, None is returned.
James Bond
These in-built items() in Python will return a tuple containing both the key and values.
usa : english
canada : ('english', 'french')
brazil : spanish
france : french
china : mandarin chinese
australia : english
russia : russian
spain : spanish
japan : japanese
italy : italian
This in-built keys() method in Python will return all the keys in the Dictionary wrapped in dict_keys object. However, you can convert it into a list or a tuple using the list() and tuple() methods, respectively.
dict_keys(['usa', 'canada', 'brazil', 'france', 'china', 'australia', 'russia', 'spain', 'japan', 'italy'])
This in-built pop() method in Python allows for the deletion of an item with the specified key.
True
False
Alternatively, you can also use the del keyword to delete items.
True
False
This in-built popitem() method in Python will remove the last inserted item from the Dictionary.
True
False
The in-built setdefault() method in Python will return the value of the specified key. If the queried key does not exist, it will insert the key and return its value.
bronco
The in-built update() method in Python will update the Dictionary with the specified key-value pairs.
{'name': 'Ford', 'model': 'bronco', 'color': 'blue'}
The in-built values() method in Python will return the values of all keys in the Dictionary.
dict_values(['english', ('english', 'french'), 'spanish', 'french', 'mandarin chinese', 'english', 'russian', 'spanish', 'japanese', 'italian'])
In this chapter, we explained the concept of Dictionaries, their usage, their definition, how to access their values, categorization of data, and its appropriate methods.