Learn Dictionaries in Python with Examples

March 5, 2014 | By in PROGRAMMING
| 2 Replies More

Dictionaries are used in Python to store objects of different types. They consist in keys and values, where each key should be unique and must be of an immutable data type such as string, number or tuple.

python dictionaries

Open Python in interactive mode and type the following.

dict = {'name':'james', 'surname':'bond', 'age':31 }

dict is a dictionary with three elements. As you guys can see, each key is seperated from its own value by a colon and pairs of keys and values are seperated from each other by commas.

To access a value you need to know the key. For example If I want to know the name of the person in my dictionary called dict I do the following.


It should return the value 'james'.


What Happens When You Use A Key That Does Not Exist In The Dictionary

When you try to access values of the dictionary with keys that do not exist in that dictionary you are going to get an error. Try the following and see the output.


You should get a KeyError. The following output is what I get.

KeyError Traceback (most recent call last)
/home/oltjano/Desktop/ in ()
----> 1 dict['test']

KeyError: 'test'

How To Add New Elements In Your Dictionary

Lets suppose that after creating a dictionary you want to populate it with new elements. You can easily do it by associating a value to a new key like shown below.

dict['house'] = 13

The above method created a new element inside our dictionary. You can verify it by running the following piece of code in your python interpreter.


If you have followed every step of this tutorial until now you should get the following output after running dict.

{'age': 31, 'house': 13, 'name': 'james', 'surname': 'bond'}

How To Remove Elements From Your Dictionary

Since dictionaries are muteable it is also possible to delete an element with del like shown in the following example.

del dict['house']

Run the following to see if the element is removed from the dictionary or not.


As you can see the 'house':13 element does not exist anymore in dict.

{'age': 31, 'name': 'james', 'surname': 'bond'}

Deleting the entire elements one by one is boring and time consuming so the following can be used to delete the entire dictionary at once.

del dict

Or if you are the type of person that always wants to learn more and more here is a method that will help to remove all the entries in your dictionary.


Note: The dict.clear() does not delete the object dict. It only delets values inside dict. You can verify that by running the following.


And you will get an empety dictionary like shown below.


Some Dictionary Methods And Functions

First create a new dictionary.

linoxide = {'tutorials':('ubuntu','python','linux')}

After you have finished creating the dictionary create a new one.


We want to visit these two websites and get informed about open source world, but before doing that we need to check if they have the same tutorials or not. Since both websites have stored their categories of tutorials in dictionaries we can use the cmp function to compare the elements of both dictionaries and seee if they are the same or not.


If they have the same elements you will get 0 as an output.


If the dictionaries that are compared don't have any element in common you will get -1 as a result and if they have one or more than one element in common you will the number 1 as a result.

It makes sense to me.

Creating a copy of a small dictionary (with very few elements) is easy and can be done my hands, but what are you going to do when you have big dictionaries with thousands or million of elements and you have to create copies of them.

The copy() method will create a copy of the specified dictionary. You can create a copy of linoxide dictionary by typing the following.

linoxide_copy = linoxide.copy()

Verify if the dictionary linoxide_copy has been created.


The following method will return a list of dictionary's keys.


I get the output shown below.


What about values of the dictionary? Use the following code to get them.


You should see the following output in your python shell.

[('ubuntu', 'python', 'linux')]

There are many builtin methods for dictionaries and we can not share them all in a tutorial, but the most important thing is that you should keep experimenting yourself until you feel comfortable with every method.


Share This :

Free Linux Ebook to Download

Comments (2)

Trackback URL | Comments RSS Feed

  1. Luke says:

    A dict is a python standard type - it's probably not the best practice to overwrite it by using it as a variable name ie "dict = {‘name’:'james’, ‘surname’:'bond’, ‘age’:31 }"

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.