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 dictionary
d1 = {'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 dictionary
print(d1)
print(d2)
print(d3)

# add element to dictionary
print("Dictionary after adding element (k: 11): ", d1.update({'k': 11}))

# remove element from dictionary
print("Dictionary after removing element (a removed): ", d1.pop('a'))

# check if key exists in dictionary
print("Is key 'a' exists in dictionary: ", 'a' in d1)

# iterate over dictionary
for key, value in d1.items():
print(key, value)

# concatenate multiple dictionaries
d4 = {**d1, **d2, **d3}
print("Concatenated dictionary: ", d4)

Output