Dictionary in Python

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

1. get()

To avoid that error we can use the get method, which will return None if the key doesn’t exist.

2. keys()

To access the keys of the dictionary.

3. values()

To access only values from the dictionary.

4. items()

It will return the key and value pairs.

5. len()

It will return the length of the dictionary.

Deletion and updating of element

6. del

7. pop()

Unlike a list, it is taking necessary one key for deletion

8. update()

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.

Close Menu