What is a Python dictionary?
A Python dictionary is a common and useful data type that structures its data with a key and value pair. It is mutable and does not allow duplicate values. In this article, we are going to cover how to create a dictionary, update and delete keys and also a little bit about how we use it in the real world.
Creating a Python dictionary
The first step involves choosing the name of the Python dictionary and assigning it to a variable. In this article, we’ll create a dictionary by the name “elephant” and then we need to equate this name to a pair of curly brackets which in Python dictate that this is going to be a dictionary.
Within the curly braces, we are going to create a pair of elements separated by a colon, the first one being the Key and the second one being the Value as shown here: “Key : Value”. While values can be of any data type and allow duplicates Keys are immutable and cannot be duplicated. The “elephant” dictionary can be created as shown below, however, we are going to split the keys and values into separate lines for simplicity and ease in reading.
Now both numbers and strings can be keys in this case and the values can be whatever you like. Each pair is separated by a comma to add another pair hit enter and type it in the next line. “Age” is the next key with a corresponding value of 25 which is then followed by “weight” : 4000 and “type”: “African” pairs each occupying their separate lines. This is how a Python dictionary is created; we have four separate keys associated with four values.
Referencing a specific key
Items in a Python dictionary can be accessed by referring to their keys inside square brackets, we can then output their values using the print statement. As shown in the code below.
Deleting and updating elements in a Python dictionary
But what if we wanted to remove a key or value from this dictionary? Well we can do that quite simply by doing “del” for delete and then calling the dictionary we want to delete the value from and again the square brackets and then the key that we want to delete.
We are going to print the whole dictionary beforehand and then afterwards and we’ll see what has been removed from the dictionary. Suppose we want to delete the key “age” and its respective value we can do so by running the code del elephant[‘age’].
Now if we proceed to print out the dictionary again we can see that it has been removed.
We can also add elements to an existing Python dictionary using the built-in method “.update()”.This can be done nicely and simply by just running this code elephant.update() and passing the pair of “Key : Value” that we wish to update. As illustrated in the code below.
In the output above the first line is the original dictionary before we made any changes, the second line is the output after we have removed age from the dictionary. Finally, the last line is the output after updating the age from 25 to 20.
Please note that in the last output the code is not able to find the key “age” that needs to be updated since we had deleted it in the preceding step. However, the code proceeds to add it like a new pair.
Creating a list of Dictionaries
In the real world of programming more often than not you’ll encounter a list of dictionaries. As much as they might seem more complex please note that they are quite simple. Using the preexisting dictionaries, let’s add another elephant by the name: “Jessica”, “age”: 20 with a “weight” of 3500 and of Indian type.
To print elements from either of the dictionaries is a bit different, but it simply involves using two square brackets, the first one defining the dictionary to look up information while the second one references a specific key in that particular Python dictionary. As illustrated below.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.