top of page
  • Writer's pictureEkta Aggarwal

Dictionaries

Python has a special object dictionaries which store the mapping in form of key and values, where value(s) map to a single key. In this tutorial we shall learn about dictionaries in depth.

Keys in dictionaries must be unique and are case -sensitive i.e. X and x will be treated as 2 different keys.


Dictionaries are denoted by { } curly brackets.

Following is an empty dictionary.

my_dict = {}

Let us create our first dictionary with key value pairs denoting Countries and their capitals.

The mapping between keys and values are denoted by : colon .

Different key-value pairs are separated by a comma.

my_dict = {'U.S.A' : 'Washington D.C',
'U.K.' : 'London',
'Japan':'Tokyo',
'Russia':'Moscow'}

type(my_dict)




Items


Items in a python dictionary are the key-value pairs. In the above dictionary we have 4 items. Items can be retrieved by:

my_dict.items()

Keys


Keys in dictionaries are unique and are used as one to many or one to one mapping for values. In our dictionary we have defined countries as our keys.

my_dict.keys()

Values


Values in a dictionary can be a one-dimensional list or single values of any variable type (numeric or string or boolean). They can be repetitive and get mapped to keys.

In my_dict we have defined capitals as the values.

my_dict.values()

Adding an item to our dictionary


We can add a new item (key-value pair) in following syntax:

dict_name['key'] = values.


Task: Add a new key - value pair 'India : New Delhi' to our dictionary my_dict.

my_dict['India'] = 'New Delhi'
my_dict








Deleting an item from our dictionary


We can delete a key value pair from our dictionary using del keyword with following syntax:

del dictionary_name['key_to_be_deleted']


Task: Delete key 'India' from our dictionary.

del my_dict['India']
my_dict







Mapping multiple values to a single key.


In a dictionary we can map multiple values to a key. The multiple values are passed in the form of a list!

Let us create a dictionary with 2 keys: Countries and Capitals, where they values will be passed on the form of lists.

my_dict = {'Countries' : ['U.S.A.','U.K.','Japan','Russia'],
          'Capitals' : ['Washington D.C.','London','Tokyo','Moscow']}
my_dict
my_dict.items()
my_dict.keys()
my_dict.values()


Dictionary inside a dictionary


We can pass a dictionary to our keys as well!

Let us create a dictionary named countries where keys are the country names and values are dictionaries containing capital and population info.

countries = { 'U.S.A.': { 'capital':'Washington D.C.', 'population':330.42 },
           'U.K.': { 'capital':'London', 'population':60.6 },
           'Japan': { 'capital':'Tokyo', 'population':126.2 }
            }

Task: Print out the capital of U.S. A.

print(countries['U.S.A.']['capital'])






Creating sub-dictionary


Let us create a new sub-dictionary called data which we will use to create a new key named India.

data = {'capital':'New Delhi','population': 1380.2}

Add data to countries under key 'India'

countries['India'] = data
print(countries)

bottom of page