Write a program to perform below operations on dictionary: ● Create a dictionary. ● Print dictionary items. ● Add/remove key-value pair in/from a dictionary. ● Check whether a key exists in a dictionary. ● Iterate through a dictionary. ● Concatenate multiple dictionaries.
info = {
'name':'mrcoder',
'roll':21,
'spi':10
}
print(info)
# alternate methode --> info.update({'sub':'python'})
info['sub'] = 'python'
del info['spi']
print(info)
key = 'name'
if key in info:
print("Key exist")
else:
print("key dose not exist")
print(info['name'])
print(info.items())
info2 = {
'skill':'python',
}
info.update(info2)
print(info)
output:
{'name': 'mrcoder', 'roll': 21, 'spi': 10}
{'name': 'mrcoder', 'roll': 21, 'sub': 'python'}
Key exist
mrcoder
dict_items([('name', 'mrcoder'), ('roll', 21), ('sub', 'python')])
{'name': 'mrcoder', 'roll': 21, 'sub': 'python', 'skill': 'python'}
Comments
Post a Comment