Topics

Dictionaries in Python

Welcome to another tutorial, here you learn about dictionaries in Python. You will also, be introduced to other useful functions that are used in manipulating dictionaries and how dictionaries can be used.

A dictionary is like an extra parameter called keys. This is quite similar to a list and strings, where we use the index as the parameter to access each element of either of them. However, the distinct feature between a list and a dictionary is that keys are used to access the elements of a dictionary.

In addition, unlike an index, a key can be of any data type varying from integer to string. This feature makes them more flexible to be used.

 

Creating a Dictionary

As you already know, the key brings flexibility when coding with a dictionary, as such we have to define each key distinctly. The table below gives an example of how an element in a dictionary is linked to a key.

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

 

The example below shows how a dictionary in python can be created.

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

From the example above, you can see a curly bracket that is used, unlike the square bracket in the list. suppose, you want to access any element of a dictionary, you must know the key for that element. Like in the above example, to access an element of Key-3, you have to use:

>>>print( myDictionary['Key-3']);

Output:

'Element-3'

 

In addition, every single element must have its unique key, because a key is used to identify each element in a dictionary, but the reverse is not possible. That is to say, elements can be repeated, but the key cannot be repeated as it is unique.

 

Dictionary with integer keys:

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

Output:

'Python'

 

Dictionary with string as keys:

>>> identity = {"name": "TutorialWithExample.com", "type": "Tutorial", "link": "https://www.tutorialwithexample.com/", "tag": "Tutorial with all examples"}
>>> print (identity['name'] + ": " + identity['tag']);

Output:

TutorialWithExample.com: Tutorial with all examples

 

Now, if we want to create an empty dictionary:

>>> emptyList = {}

From the example above, the line of code successfully initialized an empty dictionary. Also, elements can be added easily to an empty dictionary after its initialization. Check the example below:

>>> emptyList["Cars"] = "BMW"

So, the element above can be appended to the dictionary.

>>> print(emptyList);

Output:

{"Cars": "BMW"}

 

Accessing elements of a dictionary 

We can also access the elements stored in a dictionary, like List by using the for a loop. But, if we iterate over each element, the key will be gotten instead of the value of the element. So, for you to access the value of the element, you can use the key. This is shown below:

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

Output:

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 

We can use the del keyword to delete elements. This is quite similar to that done with a list. check out the example below:

>>> identity = {"Comapny": "BMW", "type": "Car", "link": "https://www.bmwusa.com/", "tag": "The New iX"}

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

>>> del identity["link"]

will delete that element.

>>> print (identity);

Output:

{"Comapny": "BMW", "type": "Car", "field": "tag": "The New iX"}

 

Appending element(s) to a dictionary

This is a situation where you need to add an extra element to your already initialized list which has elements. Check out the example below:

>>> identity["email": "bmwgenius@bmwusa.com"]

It will be added to the dictionary like this:

>>> print (identity);

Output:

{"Comapny": "BMW", "type": "Car", "field": "tag": "The New iX","email": "bmwgenius@bmwusa.com"}

 

Updating existing element(s) in a dictionary

The function update(), is typically used to merge two dictionaries. In so doing the common values of the two list is overwritten by the latter dictionary. Check out the example below:

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

From the example above, if we want to copy all elements of courseAvail to the list identity, we can do it as shown below:

>>> identity.update(courseAvail)

Note: It is important to know that from the example, the dictionary identity will get updated, while the dictionary courseAvail will not be affected.