append key and value to dictionary python code example

Example 1: python dictionary add key-value pair

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

Example 2: 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 3: add value to dictionary python

dict[key] = value

Example 4: python append to dictionary

dict = {1 : 'one', 2 : 'two'}
# Print out the dict
print(dict)
# Add something to it
dict[3] = 'three'
# Print it out to see it has changed
print(dict)

Example 5: Python dict add item

# This automatically creates a new element where
# Your key = key, The value you want to input = value
dictionary_name[key] = value

Example 6: python how to add a new key to a dictionary

# Basic syntax:
dictionary['new_key'] = value

# Example:
d = {'a': 1, 'b': 5} # Define dictionary
d['c'] = 37 # Add a new key to the dictionary
print(d)
--> {'a': 1, 'b': 5, 'c': 37}