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.
Code
# create dictionaryd1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}d2 = {'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}d3 = {'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15}# print dictionaryprint(d1)print(d2)print(d3)# add element to dictionaryprint("Dictionary after adding element (k: 11): ", d1.update({'k': 11}))# remove element from dictionaryprint("Dictionary after removing element (a removed): ", d1.pop('a'))# check if key exists in dictionaryprint("Is key 'a' exists in dictionary: ", 'a' in d1)# iterate over dictionaryfor key, value in d1.items():print(key, value)# concatenate multiple dictionariesd4 = {**d1, **d2, **d3}print("Concatenated dictionary: ", d4)
0 Comments
Post a Comment