In this article, we will learn about dictionary data structure in python. Dictionary is also known as hash maps. Hash maps data structure is the type in which values are stored corresponding to a key, and each value is extracted from the value of its key. Python dictionary is similar to the JSON data structures.
This is all the introduction of the dictionary now we will know about the details of the dictionary.
How to initialize the dictionary:
This is the first method to initialize the dictionary. We first make empty curly braces and then with each key we assign its value. In the above example we have created three keys name, type and species each key is then assigned a value and when we print the dictionary following output is given:
As we can see each key-value pair can be seen separated by the colon.
The second way to initialize the key is by using the dict () function. Following is the code to implement such a method:
The dict function does the same as the empty curly braces.
The third way to initialize the dictionary is by converting the list to the dictionary. It is done in the following way:
The above code will also act the same as mentioned above. The output will be the same, but in this case, a list contains tuple. The first value of tuple will be converted to key and the second to value.
Accessing the values:
In dictionaries, values can be accessed by multiple methods. We will now see how to access the value of a dictionary in python
Value can be accessed by the key. With the key attribute, we have to give the key name in square brackets or a get function. Following is the way to access the value:
With the important method, we can access the value by these two methods.
Iteration on Dictionaries:
We can also iterate for loop over the dictionary. When for loop is iterated over a dictionary it returns a key value, and by placing the key within square brackets, the value is extracted. Following is the method to get keys:
The above code will output the key as follows:
Now if we change the code a little bit and give these keys to the data variable, we will get the values:
Dictionary comprehension is a way to make a dictionary elegantly. We don’t use the conventional way of creating a dictionary by looping and assigning the value to it. Instead, we make a dictionary is just one line as we have seen in a list comprehension
In the above example for each number as a key, there is a corresponding value which is the square of the key. The output of the above statement is as follows:
A list within dictionaries:
We cannot just only give the value to the key in dictionaries. We can also give the list within the dictionary.
Following is the way to give the list in the dictionary:
Similarly, we can give tuple, sets and multidimensional lists in a dictionary
A nested dictionary is a dictionary that contains dictionaries within a dictionary. Just like a JSON object we can create a nested dictionary. This allows us to hold more complex data. For example, if somebody sends multiple messages, then we will save the data about the user in one key and multiple messages in multiple dictionaries. This will make it easier to manage the data. Nested dictionaries also allow holding data of different object within the same dictionary. For example, we have 10 students in a class, and each student has a unique roll number. So, by assigning the roll number as a key, we can store all the data of that particular student in a dictionary. Following is the code of the above example:
Here the 1 2 and 3 are the registration number and corresponding to each registration number is the information of that student. Now by unique id student, data can be fetched easily, and there will be no conflicts within it.
Sorted return the list of key values. Following is the example of sorted with dictionaries:
len function returns the length of the dictionary:
How to get the values of all the keys:
We can get the value of keys by the dictionary using the following code:
Sort the dictionary by key:
Sorting is one of the main issues in the dictionary. Sorted function only returns the sorted value. To do the proper sorting of the dictionary, the following method is used to sort the dictionary:
Sort Dictionary by value:
Another major problem is to sort by the value. Sort by value is little complicated than sort by the key:
As it will return the sorted list with tuples in it so we then again have to convert it into the dictionary.