append to new dictionary python code example
Example 1: 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
Example 2: add value to dictionary python
dict[key] = value
Example 3: dictionary append value python
d = {1:2}
d.update({2: 4})
print(d) # {1: 2, 2: 4}
Example 4: append dictionary python
>>> d1 = {1: 1, 2: 2}
>>> d2 = {2: 'ha!', 3: 3}
>>> d1.update(d2)
>>> d1
{1: 1, 2: 'ha!', 3: 3}