Add Values to Python Dictionary: 2 Easy Methods

To add values to a dictionary, you can use the update() method or directly use index assignment. Here are examples of both methods:

  1. refresh()
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict.update({'key3': 'value3'})
print(my_dict)

The output result is:

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
  1. Assigning values using indexes:
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_dict['key3'] = 'value3'
print(my_dict)

The output is:

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

No matter which method you choose, it is easy to add new key-value pairs to the dictionary.

bannerAds