add a key-value pair to a map in python

In Python, a Map is referred to as a Dictionary. To add a new key-value pair to a dictionary in Python, you can simply use the dictionary[key] = value syntax, where dictionary is the name of the dictionary, key is the new key you want to add and value is the corresponding value.

Here's an example:

main.py
# create a dictionary
my_dict = {'apple': 2, 'banana': 3, 'orange': 4}

# add new key-value pair to dictionary
my_dict['grape'] = 5

# print the updated dictionary
print(my_dict)
179 chars
9 lines

In this example, the new key-value pair 'grape': 5 is added to the dictionary 'my_dict'. The output of the program will be:

main.py
{'apple': 2, 'banana': 3, 'orange': 4, 'grape': 5}
51 chars
2 lines

gistlibby LogSnag