5 Dictionaries
5 Dictionaries
What is Dictionary?
A Dictionary is an unordered collection of elements in the form of key and value pair. All
elements are kept within Curley bracket. Dictionary is mutable.
Example-1
D={10:’Rahul’,20:’Deepak’,30:’Swagat’}
Example-2
D={“January”:31, “February”:28,”March”:31}
IMPORTANT NOTE:
Keys can’t be repeated but values can be.
TRAVERSING A DICTIONARY
Traversing means accessing of each element of a dictionary.
Example:
D={1:’Rahul’,2:’Deepak’,3:’Swagat’}
for key in D:
print(key,’:’,D[key])
print(D.keys())----------1,2,3
print(D.values())-------Rahul, Deepak, Swagat
NESTING DICTIONARY
A dictionary within another dictionary is called nesting dictionary
Example:
Emp={‘R.K.Singh’: {‘age’:40,’salary’:60000}, ‘B.K.Dutta’:{‘age’:42,’salary’:55000} }
print(‘age’ in Emp)---------True
print(‘dept’ in Emp)-------False
print(‘age’ not in Emp)--------False
print(‘dept’ not in Emp)-------True
DICTIONARY FUNCTIONS/METHODS
i)len()-It will print the length of a dictionary.
Emp={'name':'Suresh', 'salary':50000, 'age':37}
print(len(Emp))---------3
Emp[‘dept’]=’sales’
print(len(Emp))-------4
OUTPUT:
‘name’:’Suresh’
‘salary’:50000
‘age’:37
Example2:
Marks={1:50, 2:100, 3:200, 4:300}
Marks.setdefault(5)
print(Marks)------- {1:350, 2:423, 3:411, 4:300, 5:None}
OUTPUT:
{'Name': 'B.K.Mishra', 'Salary': 90000, 'age': 43, 'Dept': 'Sales'}
HOW CAN YOU MAKE TRUE COPY OF A DICTIONARY USING copy() function?
Example:
Emp1={“Name”:”R.K.sahoo”, “Salary”:85000,”age”:43}
Emp2=Emp1.copy()
HOW TO DELETE THE LAST ELEMENT FROM THE DICTIONARY USING popitem() FUNCTION?
Emp={“Name”:”R.K.sahoo”, “Salary”:85000,”age”:43}
Emp.popitem()
Output:
(‘age’,43)
print(Emp)---- {“Name”:”R.K.sahoo”, “Salary”:85000}
Example2:
Emp={"Name":"R.K.sahoo", "Salary":85000,"age":43}
del Emp
print(Emp)
output:
name ‘Emp’ is not defined
Ans.
D={'0':'Zero','1':'One','2':'Two','3':'Three','4':'Four','5':'Five','6':'Six','7':'Seven','8':'Eight','9':'Nine'}
n=input("Enter a number in digits: ")
for i in n:
print(D[i],end=' ')
Q4. Repeteadly asks the user to enter the team name and how many games the team has own and
how many they lost. Store these information in a dictionary where the keys are team names and
the values are [wins,losses].
a)Allow user to enter team name and print out the teams winning percentage.
Ans.
b)Create a list whose entries are the number of wins each team.
Ans.
Q5. Write a program that repeatedly asks the user to enter the product name and prices. Store all
of these in a dictionary whose keys are the product names and whose values are the prices.
When the user is done entering products and prices, allow them to repeatedly enter a product
name and print the corresponding price or a message if the product is not in the dictionary.
Ans.
D={}
ch='y'
print(D)
Q6. Create a dictionary whose keys are month names and whose values are the number of days in
the corresponding months.
(a)Enter the month name and display how many days are there.
Ans.
D={'January':31,'February':28,'March':31,'April':30,'May':31,'June':30,
'July':31,'August':31,'September':30,'October':31,'November':30,'December':31}
D1=sorted(D)
print(D1)
(d)Print all the (key:value) pairs sorted by the number of days in each month.
Ans.
D={'January':31,'February':28,'March':31,'April':30,'May':31,'June':30,
'July':31,'August':31,'September':30,'October':31,'November':30,'December':31}
for key in D:
if(D[key]==28):
print(key,':',D[key])
for key in D:
if(D[key]==30):
print(key,':',D[key])
for key in D:
if(D[key]==31):
print(key,':',D[key])
Q7. Can you store the details of 10 students in a dictionary at the same time? Details includes Roll,
Name and Marks, Grade etc. Give example to support your answer.
Ans.
D1={‘Roll’:1,’Name’:’Rahul’,’Marks’:94,’Grade’:’C’}
Assign 9 more records of student like this
Print(D)
OR
D={}
for i in range(1,3):
Q8. Given a dictionary X={‘k1’:’v1,’k2’:v2,k3:v3}, Create another dictionary with opposite mapping
as follows:
Y={‘v1’:’k1’,’v2’:’k2’,’v3’:’k3’}
Example:
D={k1:v1,k2:v2,k3:v3,k4:v4}
D={v1:k1,v2:k2,v3:k3,k4:v4}
Ans.
D={'Roll':1,'Name':'Ravi','class':'XI'}
print(D)
D1={}
for key in D:
D1[D[key]]=key
D=D1
print(D)
Q9.Given two dictionaries d1 and d2. Write a program that lists the overlapping keys of two
dictionaries i.e. list if a key of d1 is also a key of d2. List these keys.
Ans.
d1={1:11,2:22,3:33,4:44,5:55}
d2={6:66,7:77,1:111,2:222,8:88,5:555}
for key1 in d1:
for key2 in d2:
if key1==key2:
print(key1)
Q10.WAP that checks if two same values in a dictionary have different keys. That is, for dictionary
D1={‘a’:10,’b’:20,’c”:10}, the program should display ‘two keys have same values’
D2={‘a’:10,’b’:20,’c”:30}, the program should display ‘no keys have same values’
Ans.
d={'a':10,'b':20,'c':10}
ctr=0
for k1 in d:
for k2 in d:
if(d[k1]==d[k2]):
ctr+=1
if(ctr>1):
print(ctr,"keys have same value")
break
Ans.
D1={1:11, 2:12}
D2={1:11,2:12,3:13,4:14,5:15}
ctr=0
for key1 in D1:
for key2 in D2:
if key1==key2 and D1[key1]==D2[key2]:
ctr+=1
if(ctr==len(D1)):
print("D1 is present in D2")
else:
print("D2 is not present in D2")
Q12.A dictionary D1 has in the form of list of numbers. Write a program to create a new dictionary
D2 having same keys as D1 but values as the sum of the list elements.
Example:
d1={'A':[1,2,3],'B':[4,5,6]}
the d2 should be
d2={‘A’:6,’B’:15}
Ans.
d1={'A':[1,2,3],'B':[4,5,6]}
d2={}
print("d1=",d1)
print("d2=",d2)
-O-