Working With Dictionaries In Python

Dictionaries in pythons are a collection of key-value pairs. They are very similar to JSON data types in JavaScript. Dictionaries are indexed, we can modify them, and they are no ordered. This makes it very flexible and useful. Since we can access dictionary items with keys instead of indexes, dictionaries are widely used in external data-driven programs and apps.

Follow full Python series.

Learn Python Programming

Creating a dictionary

We already know how to create lists; they are comma-separated values. Dictionaries are also comma-separated key-value pairs. Dictionaries in Python are declared very similar to JSON. Have a look at the following code.

img 5d266c3b4348f

You can see in the code above that dictionaries are basically in the form of key: value with multiple elements being separated with a comma. It is important to note that no 2 items in the dictionary can have the same key. For example, in the above example, there cannot be another key named name.

Dictionaries print to console in the same way they were created. The output is as follows.

img 5d266c3c3c7e3

Accessing Items from a dictionary

Dictionaries are key-value pairs. So basically, you will be storing your data in values and referencing them with keys. To access an individual element from a dictionary, we can use the following code.

img 5d266c3c6b286

This will print out Rishabh on a terminal screen. The approach used above is very similar to how we call list items. There we use the index of the item, and here we use keys.

Looping through a dictionary

We can use the for loop to loop through a dictionary. Test out the following code in your system.

img 5d266c3ca8acd

As shown in the code above, you can run a loop to get all keys, only to get values or to get both of them.

Reassigning keys and length of a dictionary

Dictionaries cannot have duplicate keys. If you try to assign a value to some key in a dictionary, the dictionary checks if it exists. If the key exists, it is updated with the new value. If the key doesn’t exist, it will create a new key.

img 5d266c3ccd60f

The length of a dictionary is equal to the total number of key-value pairs. It can be calculated with the len() function.

Deleting Keys in a dictionary

Deleting keys is crucial as sometimes you may not want extra keys lying around. The following code deletes 2 keys using 2 different techniques.

img 5d266c3d26198

You can see that we’ve written a \n in out print statement; it puts the print cursor to a new line. We have used the del keyword to delete the age key and the pop() function to delete the name key.

The output should show that both keys have been deleted.

img 5d266c3d68015

Conclusion

Dictionaries are very useful in working with external data stores. No 2 keys can be the same, and this ensures uniqueness and integrity. Dictionaries are very flexible and allow for any data type. They can be modified and looped through very easily. I am sure you have a decent understanding of dictionaries. If you have any doubt regarding this topic, drop it in the comment section below.

SHARE THIS POST

MassiveGRID Banner
2 Comments Text
  • Great tutorial Sandy! Can you post another one for tips on approaches to tackle nested JSON dictionaries?

  • Leave a Reply

    Your email address will not be published. Required fields are marked *