alexgbelov
0
Q:

how to add an item to a dictionary in 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) 
4
dict[key] = value
14
d={'a': 1,'b':2,'c':3}
d['d']=100                # to add  keys and values in dicionary 
1
a_dictonary = {}
a_dictonary.update({"Key": "Value"})
0
# This automatically creates a new element where
# Your key = key, The value you want to input = value
dictionary_name[key] = value
1
d = {'key': 'value'}
print(d)
# {'key': 'value'}
d['mynewkey'] = 'mynewvalue'
print(d)
# {'key': 'value', 'mynewkey': 'mynewvalue'}
0

New to Communities?

Join the community