Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of the dictionary. Let us have a look at them in detail.
In python, dictionaries are used by programmers to store data in a particular format. This format is in the form of key-value pairs. They have a few specific properties, let’s have a brief look at them.
In python, data stored in dictionaries is changeable, that is, you can make changes (adding, updating, or removing items) to the data even after it is initialized.
Dictionaries in python do not allow the storage of duplicate items (two items with the same key cannot be stored in the same dictionary).
One important thing to note is that dictionaries store data in an ordered format in versions after python 3.7. Before that, dictionaries stored data in an unordered format.
Each item in a dictionary is stored in a key:value pair. We can refer to each item using the key name.
The syntax of dictionaries is very straightforward and is as follows. We simply use comma-separated key-value pairs enclosed within curly brackets. Each key and value are separated by a colon(:).
{<key_1> : <value_1>, <key_2> : <value_2>........}
We create a dictionary by first deciding a variable name, here dict. We assign the dictionary to it after creating it using the syntax specified above. ‘a’, ‘b’ and ‘c’ are the keys here and 1, 2, and 3 are the corresponding values.
These key-value pairs are separated by commas and all of them are enclosed within curly brackets.
Let us look at the python code and corresponding output for this example:
# Creating and initializing the dictionary
dict = {'a':1, 'b':2, 'c':3}
# PrintingÂ
print(dict)
Output
{'a': 1, 'b': 2, 'c': 3}
Accessing items of a dictionary is similar to how it is done in other python structures like lists.
To access a value of a dictionary, we use its corresponding key.
In this example, if we want to access the value 1, we do it using its corresponding key a and then print it.
Let us look at the python code and corresponding output for this example:
# Creating and initialising the dictionary
dict = {'a':1, 'b':2, 'c':3}
# PrintingÂ
print(dict['a'])
Output
1
As mentioned before, we can change the items in a dictionary.
To change the value corresponding to the key a from 1 to 5, we simply reassign it.
Let us look at the python code and corresponding output for this example:
# Creating and initializing the dictionary
dict = {'a':1, 'b':2, 'c':3}
# PrintingÂ
print(dict)
# Modifying
dict['a'] = 5
# Printing
print(dict)
Output
{'a': 1, 'b': 2, 'c': 3}
{'a': 5, 'b': 2, 'c': 3}
As mentioned earlier, dictionaries do not allow duplicates, as in, having different values for the same key in the same dictionary. Here we try to have both 1 and 5 as the value for the key a. As you can see in the output, the value 1 is ignored and the value 5 is considered. As a general rule, the latest or rightmost value is considered and the earlier one is ignored. Let us look at the python code and corresponding output for this example -
# Creating and initialising the dictionary
dict = {'a':1, 'b':2, 'c':3, 'a':5}
# PrintingÂ
print(dict)
Output
{'a': 5, 'b': 2, 'c': 3}
In some cases, it might be helpful to be able to access the length of a dictionary, especially when the number of items in the dictionary is very large. Here, we used the len() function to get the length of the dictionary.
Let us look at the python code and corresponding output for this example:
# Creating and initializing the dictionary
dict = {'a':1, 'b':2, 'c':3}
# PrintingÂ
print(len(dict))
Output
3
In some cases it might be helpful to be able to remove items from a dictionary. We can achieve this using the in-built pop() function.
The corresponding key of the desired key-value pair to be removed is passed as a parameter to this function.
Let us look at the python code and corresponding output for this example:
# Creating and initializing the dictionary
dict = {'a':1, 'b':2, 'c':3}
# PrintingÂ
print(dict)
# Removing an item
dict.pop('a')
# Printing
print(dict)
Output
{'a': 1, 'b': 2, 'c': 3}
{'b': 2, 'c': 3}
In some cases it might be helpful to be able to create a nested dictionary in python, depending on the way your data is to be structured.
Here, we consider an item in a dictionary with key as a and corresponding value as another dictionary with two items.
Let us look at the python code and corresponding output for this example:
# Creating and initializing the nested dictionary
dict = {'a':{'b':1, 'c':2}}
# PrintingÂ
print(dict)
Output
{'a': {'b': 1, 'c': 2}}
In this topic, we have learned the use and advantages of dictionaries in a Python, following a running example of a simple dictionary code, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.