add new key in dictionary python
dict = {'key1':'geeks', 'key2':'for'}
print("Current Dict is: ", dict)
# adding key3
dict.update({'key3':'geeks'})
print("Updated Dict is: ", dict)
# adding dict1 (key4 and key5) to dict
dict1 = {'key4':'is', 'key5':'fabulous'}
dict.update(dict1)
print(dict)
# by assigning
dict.update(newkey1 ='portal')
print(dict)
# Let's create a dictionary, the functional way
# Create your dictionary class
class my_dictionary(dict):
# __init__ function
def __init__(self):
self = dict()
# Function to add key:value
def add(self, key, value):
self[key] = value
# Main Function
dict_obj = my_dictionary()
dict_obj.add(1, 'Geeks')
dict_obj.add(2, 'forGeeks')
print(dict_obj)