Dictionary is like associative arrays in other languages. In that, we have a key and value pair. We can access the value with the use of key and insert any no. of elements. It is useful when we want to access with keys(or some name). Let me give one example.
Suppose you have a list of students with their name, roll no. and email id and you want to access only the name then you don’t need to bother about the index of the name entries of you using a dictionary. In the dictionary you can say dict_name[‘name’] then you will be able to access easily.
How to create a dictionary
How to access if that key doesn’t exist
To avoid that error we can use the get method, which will return None if the key doesn’t exist.
To access the keys of the dictionary.
To access only values from the dictionary.
It will return the key and value pairs.
It will return the length of the dictionary.
Deletion and updating of element
Unlike a list, it is taking necessary one key for deletion
The update will add new values and it will also update old key and value pairs.
Here you can see one interesting thing that this dictionary is not showing in the order we inserted elements. In some cases, we need in the order we inserted. So, how to achieve it. Python makes it simple only, we have to use the orderdict inbuilt function. Let’s take a look at it.
This is all for the dictionary, Hope you are able to understand how to access a dictionary.