Python Labs : Basic Data Structures [Dictionary]
In this article, I will explain what is a Python dictionary and the basic characteristics of it and how to access and manage the data stored in a dictionary.
A dictionary is a collection of data which is unordered and changeable. When I say unordered it means access to the items in a dictionary doesn't depend on the order they are stored. However, Python guarantees that the order of the items in a dictionary to be preserved as they were defined. But they are changeable at any given point of time. You can print a dictionary and see that the items will be displayed in the order they were defined. See the capital_cities
dictionary in Figure 1 below.
Further, dictionaries use a concept call key-value pairs. As you can see in the figure above (Figure 1) you can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces (there is another way where you can use dict
function which I have shown in the Sample_Code_1). In the example shown above, keys are the countries and values are the cities. A colon (:
) is used to separates each key from its associated value. This the way that dictionaries stores your data. Also, the iterations through the keys will occur in the order they are defined. Once you add a new item(key-value pair) that will be added at the end. When you delete an item the order of the remaining items retained as it is.
It is important to keep in your mind that when you are defining a dictionary key, make sure to use a hashable object. "Python’s immutable built-in objects are hashable". Simply what this means, the object that you are using as the key should be able to pass to a hash function. Python’s built-in hash()
function returns the hash value for an object which is hashable and throws an exception for an object which isn’t. For example, dictionaries and list are not immutable so you can not use those as your dictionary key, yet you can use, tuples, strings, int and all the other immutable objects. Refer to Figure 2.
I will talk about how to retrieve, store, delete and update items in a dictionary through sample codes. Thus, while keeping all your doubts let's check some sample codes and see whether that'll help. You can copy and paste this in your favourite Python IDE/editor and run it. Go through the comments to understand what the code actually does (Reading comments and understanding code is a good practice in the long run). Refer Sample_Code_1.
Now you should be able to understand how to define a dictionary and add, update, delete values/items in it. Let's look and some operators and commonly used and built-in functions related to Python dictionaries. Refer Sample_Code_2
I hope you are familiar with how to create a dictionary and do some preliminary tasks around it and some commonly used functions. Let's look at some of the characteristics of dictionaries before we conclude. It is important to notice that dictionaries carry similar characteristics to a list. But there are differences as well. If you wanna read further about list refer "Python Labs — Basic Data Structures [Lists]". Firstly let's look at some similar characteristics of dictionaries and a list.
- Dictionaries can be modified. Thus we call they are mutable. So they are not hashable.
- They can grow and shrink as needed so call dictionaries are dynamic. Which means you can add as many values you want and remove as well.
- They can hold various types of items/elements and objects.
- Dictionary can store another dictionary within as values in a key-value pair. That means dictionaries can be nested.
Apart from the above characteristics dictionaries differ from data type like lists primarily in the way that the elements are accessed. Unlike in the list, dictionary elements are accessed via keys. In lists, if you remember it's the position of the element otherwise we call it via the index that we used to access an item.
Well, that's pretty much it. If I have missed anything please comment below so that I can also learn. Hope this article will help you to get a basic understanding of what are Python dictionaries and what you can do with it. Once you know that start work on some projects, do some code quizzes then you will build a good sense of when to use a dictionary data type, and how to do so. Good luck. :)
If you like this article and the content that I create, you can buy me a virtual coffee ️😊;
Cheers!