Dictionary is like associative arrays in other languages. In that we have key and value pair. We can access the value with the use of key and insert any no. of elements. It is useful when we we want to access with keys(or some name). Let me give one example.

Suppose you have list of students with their name, rollno. and email id and you want to access only the name then you don’t need to bother about the index of the name enteries of you using dictionary. In dictionary you can say dict_name[‘name’] then you will be able to access easily.

How to create dictionary

In [1]:

## Creating a dictionary
dict1 = {'one': 1, 'two': 2, 'three': 3}


{'three': 3, 'one': 1, 'two': 2}

How to access each element in dictionary

In [2]:



We cannot access through index

In [3]: As in dictionary, we cannot access the elements through index as of list.


KeyError                                  Traceback (most recent call last)
 in ()
----> 1 print(dict1[0])

KeyError: 0

How to acess if that key doesn’t exist

In [4]:

## It will generate a keyerror


KeyError                                  Traceback (most recent call last)
 in ()
      1 ## It will generate a keyerror
----> 3 print(dict1['four'])

KeyError: 'four'

1. get()

In [5]: To avoid that error we can use get method, which will return None if key doesn’t exist.


## You can use your custom message a well 
print(dict1.get('four','Not found'))

Not found

2. keys()

In [6]:

## This is how we can access keys of dictionary.

dict_keys(['three', 'one', 'two'])

3. values()

In [7]:

## This is how we can access the values of dictionary.

dict_values([3, 1, 2])

4. items()

In [8]:


dict_items([('three', 3), ('one', 1), ('two', 2)])

In [9]:

## Iterating over key and value pairs of a dictionary.
for key,value in dict1.items():

    ## printing key and value pair
    print("Key is {} and value is {}".format(key,value))

Key is three and value is 3
Key is one and value is 1
Key is two and value is 2

5. len()

In [10]:

## It will print how many elements are present in the dictionary.


Deletion and updation of element

6. del

In [11]:

del dict1['one']

In [12]:


{'three': 3, 'two': 2}

7. pop()

Unlike list it is taking necessary one key for deletion

In [13]:




In [14]:


{'three': 3}

In [15]:

## You can add new key and value pair like this
dict1['one'] = 1

In [16]:


{'three': 3, 'one': 1}

8. update()

Update will add new values and it will also update old key and value pairs.In [17]:

dict1.update({'one':'11', 'two':'2'})

{'three': 3, 'one': '11', 'two': '2'}

Here you can see one interesting thing that this dictionary is not showing in the order we inserted elements. In some cases, we need in the order we inserted. So, how to achieve it. Python makes it simple only, we have to use orderdict inbuilt function. Let’s take a look at it.


In [21]:

from collections import OrderedDict

dict12 = OrderedDict()
dict12['one'] = 1
dict12['two'] = 2
dict12['three'] = 3

## It will print in a order of insertion.

OrderedDict([('one', 1), ('two', 2), ('three', 3)])

This is all for dictionary, Hope you are able to understand how to access dictionary.