python dictoinary add value
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)
# Append to Dictionary in Python
# Let's say we had the following dictionary:
languages = {'#1': "Python", "#2": "Javascript", "#3": "HTML"}
# There are two ways to add a key-and-value set to this dictionary
# Number 1: By .update() method
languages.update({"#4": "C#"}) # Adds a #4 key-and-value set
#--------------------------------------------
# Number 2: The define-key method
# This is the easier one
languages['#4'] = 'C#'
# ^^ Just updates a key of #4 to C#, or adds it in this case
# 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)