Sobes.tech
Junior — Middle

How can you insert a new key-value pair into a dictionary in Python?

sobes.tech AI

Answer from AI

In Python, to insert a new key-value pair into a dictionary, simply assign a value to a new key:

my_dict = {'a': 1, 'b': 2}
my_dict['c'] = 3  # adds a new key 'c' with value 3

If the key already exists, its value will be updated. If the key is new, it will be added to the dictionary.