append key value to dictionary python code example

Example 1: add new keys to a dictionary python

d = {'key':'value'}
print(d)
# {'key': 'value'}
d['mynewkey'] = 'mynewvalue'
print(d)
# {'mynewkey': 'mynewvalue', 'key': 'value'}

Example 2: python create dictionary from key value

>>> L1 = ['a','b','c','d']
>>> L2 = [1,2,3,4]
>>> d = dict(zip(L1,L2))
>>> d
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Example 3: add item to python dictionary

data = dict()
data['key'] = value

Example 4: python dictionary add key-value pair

dict = {'key1':'value_one', 'key2':'value_two'}  
dict['key3'] = 'value_three'

Example 5: python3 add dictionary to dictionary

dict_1 = {"1":"a", "2":"b", "3":"c"}
dict_2 = {"4":"d", "5":"e", "6":"f"}

dict_1.update(dict_2) 
print(dict_1)
#Output = {"1":"a", "2":"b", "3":"c", "4":"d", "5":"e", "6":"f"}

Example 6: Python dictionary append

# to add key-value pairs to a dictionary:

d1 = {
	"1" : 1,
	"2" : 2,
  	"3" : 3
} # Define the dictionary

d1["4"] = 4 # Add key-value pair "4" is key and 4 is value

print(d1) # will return updated dictionary