Dictionary in Python
Dictionary in Python
# empty dictionary
samp_dict = {}
print(samp_dict)
# using dict()
samp_dict = dict({1:'Cricket', 2:'Tennis'})
print(samp_dict)
# from sequence having each item as a pair
samp_dict = dict([(1,'Computer Science'), (2,'Informatics Practices')])
print(samp_dict)
Creation of Dictionary:
1. Adding key: value pairs to an empty dictionary:
a. Student = {}
b. Student = dict()
c. Add key:value pairs, one at a time as follows:
Student[‘rno’] = 10
2. Using dict() functions:
a. Specify key:value pairs as keyword argument
employee = dict(name = 'Amit', dsg = 'Analyst', salary = 50000 )
print(employee)
{'name': 'Amit', 'dsg': 'Analyst', 'salary': 50000}
b. Using comma-separated key:value pairs:
employee = dict({'name':'Amit', 'dsg':'Analyst', 'salary':50000} )
print(employee)
{'name': 'Amit', 'dsg': 'Analyst', 'salary': 50000}
c. Specify keys separately and corresponding values separately in
parenthesis (as tuples)
employee = dict(zip(('name','dsg','salary'),('Amit','Analyst',
50000)))
print(employee)
{'name': 'Amit', 'dsg': 'Analyst', 'salary': 50000}
d. Specify key:value pairs separately in form of sequences:
employee = dict([['name','Amit'],['salary',50000],['dsg','Analyst']])
print(employee)
employee = dict([('name','Amit'),('salary',50000),('dsg','Analyst')])
print(employee)
employee = dict((('name','Amit'),('salary',50000),('dsg','Analyst')))
print(employee)
Update/Modifying Existing elements:
In Dictionaries, the updation and addition of elements are similar in
syntax:
employee = {'name': 'Amit', 'salary': 50000, 'dsg': 'Analyst'}
employee['name'] = 'Sachin'
print(employee)
{'name': 'Sachin', 'salary': 50000, 'dsg': 'Analyst'}
Note: If key doesn’t exist new entry will be added
o Dict.update(): This method merges key: value pairs from the new
dictionary into the original one, new items will be added and the
old one override if any items already exist with same keys.
d = {1: 'Amit', 2: 'Sachin', 3: 'Ram', 4: 'Kunnal'}
d2 = {2:'Tarun', 5:'Ajay'}
print(d)
{1: 'Amit', 2: 'Tarun', 3: 'Ram', 4: 'Kunnal', 5: 'Ajay'}
12. Below are the two lists convert it into the Dictionary:
13. Access the value of key ‘history’ from the below dictionary:
MyDict = {
"class":{
"student":{
"name":"Mike",
"marks":{
"physics":70,
"history":80
}
}
}
}
14. Check if a value 200 exists in a dictionary
sampleDict = {'a': 100, 'b': 200, 'c': 300}
15. Rename the key ‘city’ to ‘location’ in the following dictionary:
sampleDict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
16. Which of the following will result in an error for a given
valid dictionary D?
a. D + 3
b. D * 3
c. D + {3 : '3'}
d. D.update({3: '3'})
e. D.update({{'3':3}})
4. Which of the following functions will return the key, value pairs of a
dictionary?
a. Keys()
b. Values()
c. Items()
d. All of these
9. Which of the following will delete key_value pair for key =’tiger’ in
the dictionary?
d = {'lion':'wild', 'tiger':'wild', 'cat':'domestic', 'dog': 'domestic'}
a. d['tiger']
b. d['tiger'].delete()
c. delete(d.['tiger'])
d. del(d.['tiger'])
10. Which of following Python codes will give the same output if
dict = {'diary': 1,'book':3, 'novel':5}
a. dict.pop('book')
b. del dict['book']
c. dict.update({'diary':1,'novel':5})