Tuesday

Learn Python Easy way - Dictionaries

Let us see what is a dictionary in Python. Creating a dictionary is like creating a List, a list of coupled objects, pair of objects. A list is an ordered set of objects, and a dictionary is un-ordered set of objects

Objects in a list can be accessed by their index numbers but objects in a dictionary can't. Each object in a dictionary has a Key and a Value. We need to use the key to access it's value. A key woks like the index number. But a key is not necessarily a number. It can be any data type.



Let us understand a dictionary by an example.

This is a table of names of most populated cities in india with the number of it's inhabitants according to the last census.

Mumbai 12,442,373
Delhi  11,034,555
Bangalore 8,443,675
Hyderabad 6,993,262
Ahmedabad 5,577,940
Chennai 4,646,732

Let us create a dictionary using Python, so that we will then be able to type in the name of the city to find out the number of people living in it. A dictionary is enclosed by curly braces {}

I will give a short name to this dictionary "city" for ease of use

city = {"Mumbai":12442373, "Delhi":11034555, "Bangalore":8443675, "Hyderabad":6993262, "Ahmedabad":5577940}

This is a dictionary. The name of the dictionary is city. All the elements of this dictionary are within {} curly braces. Each object in the dictionary is separated by a , comma. Each element consists of a key and a value.

"Mumbai":12442373

We are using a text string "Mumbai" as a key here. A : Colon follows a key and the next comes the Value. We have a number as a value. Keys and values can have any data type. Thus we can see, we can create any type of table. A digital table which we can then query.

city["Mumbai"]
12442373

city["Bangalore"]8443675

But we will get a Error message if we try to access a name not in there

city["Kolkata"]...KeyError: 'Kolkata'

We can access all the elements in a dictionary by simply typing it's name in a Python shell

city
{'Mumbai': 12442373, 'Delhi': 11034555, 'Bangalore': 8443675, 'Hyderabad': 6993262, 'Ahmedabad': 5577940}


Let us see what are the operations that we can perform on a dictionary
Say, we want to add a new city name in it along with it's population. How do we do that ? It is shown below.


city["Chennai"] = 4646732
Let us substantiate that by printing the entire dicionary

city
{'Mumbai': 12442373, 'Delhi': 11034555, 'Bangalore': 8443675, 'Hyderabad': 6993262, 'Ahmedabad': 5577940, 'Chennai': 4646732}

How about changing the value of population of a city ? We can do that too

city["Mumbai"] = 12500000

And confirm it by

city["Mumbai"]
12500000

What if we decide to delete a city in the dictionary, could we do that ? Yes we can.

del city["Ahmedabad"]

We can confirm that by using just the city["Ahmedabad"] which will display a keyError, or we can see the entire dicionary. As our dictionary is short, we can print it here

city
{'Mumbai': 12500000, 'Delhi': 11034555, 'Bangalore': 8443675, 'Hyderabad': 6993262, 'Chennai': 4646732}

Notice that Ahmedabad is missing in there!

Now let us take another example where a Key is a number and the Value is a Text string. We might sometime need such a table

mydict = {1:"Sanjay", 2:"Chandrakant", 3:"Shailaja"}

mydict
{1: 'Sanjay', 2: 'Chandrakant', 3: 'Shailaja'}

mydict[1]
'Sanjay'

mydict[2]
'Chandrakant'







Featured Post

Creating Games in Scratch for ICSE Class 6

An Introduction to Creating Games in Scratch  for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...