Working With Dictionaries In Python

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

Follow full Python series

Lear 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.

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.

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.

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.

As shown in the code above you can run a loop to get all keys, to only 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, a new key will be created.

The length of a dictionary is equal to the total number of key-value pairs. It can be simply 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.

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.

Conclusion

Dictionaries are very useful in working with external data stores. No 2 keys can be the same, this ensures uniqueness and integrity. Dictionaries are very flexible and allow for any datatype. 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.

Leave A Reply

Your email address will not be published.

2 Comments
  1. Dave Florek says

    Great tutorial Sandy! Can you post another one for tips on approaches to tackle nested JSON dictionaries?

  2. sandy says

    Thanks Dave for reading! Yeah I can write a separate article once this series is complete.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More