In this post we will see how we could work with python dictionary, which is another data type in python that used to represent in key value pair manage in unordered manner. We could use these keys could immutable data type like string or tuple on other hand value could carry any type of values mutable or immutable.

Example of Python Dictionary like below code.

>>> dic={"India":"Delhi","Nepal":"Kathmandu","Pakistan":"Islamabad","Russia":"Moscow"}
>>> dic['India']
>>> type(dic)
<type 'dict'>

Access Dictionary values

For accessing dictionary values we can use keys in same square brackets [] like any data type with indexes. But here we use keys instead index. Let’s take some examples to understand this.

We have dictionary which has some countries and their capitals name, we can access these capitals with country names like above we did, let’s try with another country name for another its capital.

>>> dic={"India":"Delhi","Nepal":"Kathmandu","Pakistan":"Islamabad","Russia":"Moscow"}
>>> dic["Nepal"]

Updating, Adding and deleting in Dictionary

As in every immutable data type dictionary also has methods and ways to work on them to change them as per requirement. We are not going to work on many methods on this post, but yes some important function that need to know in this post will cover.

Addition in dictionary
As we mentioned earlier we have one country & capital dictionary, now we have time to add another key value like below.

>>> dic
{'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'India': 'Delhi', 'Russia': 'Moscow'}
>>> dic["Japan"]="Tokyo"
>>> dic
{'Pakistan': 'Islamabad', 'Nepal': 'Kathmandu', 'India': 'Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}

updating or editing in dictionary
suppose we already have one key value and now we change its value to some else like below.

>>> dic
{'Pakistan': 'Islamabad', 'Nepal': 'kathmandu', 'India': 'Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}
>>> dic["India"]="New Delhi"
>>> dic
{'Pakistan': 'Islamabad', 'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}

There is another method in dictionary called update which is useful to update multiple key values in existing dictionary like below

>>> dic
{'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}
>>> dic.update({"china":"Beijing","Bhutan":"Thimphu"})

>>> dic
{'china': 'Beijing', 'Bhutan': 'Thimphu', 'Japan': 'Tokyo', 'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow'}

Earlier dic has 4 pairs, we added two more in one shot and now we have 6 pairs of keys and values in same dic dictionary. So we can see how to use dictionary in python. We have plenty of methods to work on which we discover in some later series with doing some real stuff with this data type.

Deleting in dictionary
Let’s see how we can delete any exiting key and values in dictionary like below.

>>> dic
{'Pakistan': 'Islamabad', 'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}
>>> del dic["Pakistan"]
>>> dic
{'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}

We could also delete entire Dictionary as well with below way.

dict.clear()    # remove all entries in dict
del dict         # delete entire dictionary

Using Dictionary in loops

As in programming requirement many times we need to work on iteration like in for or while loop as we did earlier with other data types. So now we would see how we could work on dictionary within loops, although we have many ways to work with same, but just strict on few now.

>>> dic
{'Nepal': 'kathmandu', 'India': 'New Delhi', 'Russia': 'Moscow', 'Japan': 'Tokyo'}
>>> for key, value in dic.items():
...     print key,"----",value
Nepal ---- kathmandu
India ---- New Delhi
Russia ---- Moscow
Japan ---- Tokyo

In above code we used items method to use dictionary’s key and values. We have same kind keys and values method for them to work only.

>>> for key in dic.keys():
...     print key

So in above code we only used to call keys no values, Same could be done through values like below code

>>> for value in dic.values():
...     print value
New Delhi

That doesn’t mean we could only work like this as said in python we always another way to figure out things and simple and short is always best.

>>> for i in dic:
...     print i,"---",dic[i]
Nepal --- kathmandu
India --- New Delhi
Russia --- Moscow
Japan --- Tokyo

Dictionary properties

There are some properties of dictionary which we should consider as important part.

No duplicate key is allowed. If in case you like to update any value through existing key, value would replaced with new value with same key.

>>> month_dict
{1: 'Jan', 2: 'Feb', 3: 'Mar'}
>>> month_dict[1]="Dec"
>>> month_dict
{1: 'Dec', 2: 'Feb', 3: 'Mar'}

Dictionary keys are immutable, we could use strings, numbers or tuples as dictionary keys, but list are not allowed.

>>> month_dict[[1,2,3]]=["Jan","Feb","Mar"]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'
Error in sys.excepthook:

As seen above we try to use list as key caused error, in case we use tuple instead of same list, it will take value without any problem.

>>> month_dict[(1,2,3)]=["Jan","Feb","Mar"]
>>> month_dict
{1: 'Dec', 2: 'Feb', 3: 'Mar', (1, 2, 3): ['Jan', 'Feb', 'Mar']}

There are many builtin functions for python dictionary which we could use to work with Python dictionary datatype.