Dictionaries in Python

In this tutorial, we will learn what a Dictionary is? How it can be used? and some useful functions used for manipulating Dictionary.

Dictionaries are much like lists with an extra parameter called keys. Recall, how in lists and strings, we used the index as the parameter to access each element of the string/list. The main differentiating factor between a list and a dictionary would be, that instead of the index we use keys to access the elements of a dictionary (or values to access keys, works both ways).

Also, unlike an index, keys can be of any data type varying from integer to string. This makes them more flexible to use.


Creating a Dictionary

Since we have flexibility in providing the key for each element in the dictionary, we will have to define each key explicitly. Below we have a dictionary in tabular format. For each element in the dictionary we have a key linked to it.

KeyValue
Key-1Element-1
Key-2Element-2
Key-3Element-3
Key-4Element-4
Key-5Element-5

A dictionary in python can be created as:

>>> myDictionary = {'Key-1': 'Element-1', 'Key-2': 'Element-2', 'Key-3': 'Element-3', 'Key-4': 'Element-4'}

Notice the curly braces that are used here, unlike square braces in the list. Here Key-1, Key-2... are the keys for Element-1, Element-2... respectively. Therefore, if you want to access any element of a dictionary, you should know the key for that element. For example, to access element of Key-3, then just use,

>>> myDictionary['Key-3']

'Element-3'

Also, in a dictionary, each element must have a unique key, since a key is used to uniquely identity each element of the dictionary, however, the reverse is not true, which means that elements can be repeated, but key must be unique.


Dictionary with integer keys:

>>> integerDictionary = {10: "C++", 20: "Java", 30: "Python", 40: "Ruby", 50: "C#", 60: "Perl"}
>>> integerDictionary[30]

"Python"


Dictionary with string as keys:

>>> identity = {"name": "StudyTonight", "type": "Educational", "link": "https://studytonight.com", "tag": "Best place to learn"}
>>> print identity['name'] + ": " + identity['tag']

StudyTonight: Best place to learn

To create an empty dictionary, do the following:

>>> emptyList = {}

The above line of code successfully initialized an empty dictionary. We can easily add elements to an empty dictionary after its initialization. Suppose you want to add Delhi with key India to your dictionary, then do it like,

>>> emptyList["India"] = "Delhi"

And this element will get appended to the dictionary.

>>> emptyList

{"India": "Delhi"}


Accessing elements of a dictionary

Elements stored in a dictionary can be accessed just like lists in python, i.e, using the for loop. However, while iterating over each element, we will get the key and not the value of the element, therefore, to access the value of the element, we have to use the key just like index, For example: myDictionary[key].

for i in myDictionary:
    print "Key: " + i + " and Element: " + myDictionary[i]

Key: Key-1 and Element: Element-1 Key: Key-2 and Element: Element-2 Key: Key-3 and Element: Element-3 Key: Key-4 and Element: Element-4 Key: Key-5 and Element: Element-5


Deleting element(s) in a dictionary

Elements can be deleted using del keyword, which is similar to how its done in a list. For example, considering our website details dictionary,

>>> identity = {"name": "StudyTonight", "type": "Educational", "link": "http://studytonight.com", "tag": "Best place to learn"}

If we want to delete the link key and the value associated to it, then

>>> del identity["link"]

will delete that element.

>>> identity

{"name": "StudyTonight", "type": "Educational", "field": "tag": "Best place to learn"}


Appending element(s) to a dictionary

Suppose you want to add an extra element to your already initialized list which has elements, then, all you have to do is:

>>> identity["email": "we@studytonight.com"]

And it will be added to the dictionary.

>>> identity

{"name": "StudyTonight", "type": "Educational", "tag": "Best place to learn", "email": "we@studytonight.com"}


Updating existing element(s) in a dictionary

The update() function is used for merging two dictionaries into one. The common values of both the lists get overwritten by the latter dictionary. For example, let's assume that there is another dictionary containing the list of the available courses on StudyTonight, along with the list used in the example above.

So now we have 2 lists - identity and courseAvail

>>> courseAvail = {"Java": "Full-course", "C/C++": "Full-course", "DBMS": "Full-course"}

Suppose we want to copy all elements of courseAvail to the list identity, then we just have to do th following:

>>> identity.update(courseAvail)

Note: In this case dictionary identity will get updated, and there would be no effect on dictionary courseAvail.