Python - Dictionaries
Python - Dictionaries
Dictionaries are the structures which holds the data in key - value pair format. For example, key can be student
id and value can be student object corresponding to that student id. It allows faster access to the object as
compared to the elements stored as part of list.
Dictionaries are more general form of the lists. They are more readable and one does not to keep the index in
mind while accessing it. For example, a list of months is available, in order to access the October month, one
need to keep its index in mind. Also if days of those months also needs to be referred, then another list needs
to be maintained.
In [ ]:
month_list = ['Jan', 'Feb', 'Mar', 'Apr', "May", 'Jun', 'Jul', "Aug", 'Sep', 'Oct', 'Nov',
month_list[9] # accessing Oct
In [ ]:
days_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
days_list[9] # accessing days of Oct
Dictionaries simplifies this job by maintaing 'month names' as keys and days of months as values.
Creation of Dictionaries
It can be created with curly braces.
In [ ]:
empty_dict = {}
empty_dict
It can have key value pairs within it. Each key is separated from its value by a colon ":". Commas separate the
items, and the whole dictionary is enclosed in curly braces.
In [ ]:
my_dict = {'Jan': 31, 'Feb':28, 'March': 31, 'Apr':30} # keys are strings
my_dict
In [ ]:
In [ ]:
In [ ]:
type(my_dict)
In [ ]:
len(my_dict)
dict() function can also be used to create dictionary. List of tuples which actually contains the key value pairs
needs to be provided as the input.
In [ ]:
my_dict = {'Jan': 31, 'Feb':28, 'March': 31, 'Apr':30} # keys are strings
my_dict
In [ ]:
In [ ]:
In [ ]:
my_dict.items()
Individual value can be found out by using the key associated with it.
In [ ]:
In [ ]:
In [ ]:
my_dict['xyz'] # access number of days associated with 'xyz' , error as key is not present
In [ ]:
my_dict.get('xyz') # even if key is not present in the dictionary does not throw error
In [ ]:
'in' and 'not in' can be used to determine key is present in dictionary or not
In [ ]:
'Apr' in my_dict # check whether entry with key 'Apr' is present or not
In [ ]:
'XYZ' in my_dict # check whether entry with key 'XYZ' is present or not
Looping
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Operations on dictionary
Entry (i.e. key value pair) can be added to dictionary in following manner :
In [ ]:
my_dict['May'] = 31
my_dict
In [ ]:
In [ ]:
my_dict['May'] = 25 # change it to 25
my_dict['May']
In [ ]:
In [ ]:
#Cretae a employee database with emp id as key and list of employees where each value in li
emp_data = { 1:['1','Emp A', 34] , 2:['2','Emp B', 35], 3:['3','Emp C', 36]}
print(emp_data)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Exercise
Q1. Write a program that asks user to enter the 5 product names and prices. Store all of these in a dictionary
with keys are product names and prices are values. Then allow the user to input the product name and print the
corresponding price of the product.
In [ ]:
#Try it here
Q2. Create a dictionary where keys are name of months and days are values.
(a) Ask the user to input a month name and use the dictionary to tell how many days are in that month.
In [ ]:
#Try it here
Q3. Ask user 5 times to enter a team name and how many times the team own and how many they lost. Store
the information in a dictionary where the keys are team names and values are lists of form [wins, lossess].
(a) Using this dictionary, allow the user to enter a team name and print out the teams winning percentage.
(b) Using this dictionary, create a list whose entries are the number of wins of each team.
In [ ]:
#Try it here