Google News
logo
Python Program to Add an item to a Dictionary
In the following example of Python program that adds a new key-value pair to a dictionary :
Program :
my_dict = {"apple": 2, "banana": 3, "orange": 4}

# Add a new key-value pair to the dictionary
my_dict["pear"] = 5

# Print the updated dictionary
print(my_dict)
Output :
{'apple': 2, 'banana': 3, 'orange': 4, 'pear': 5}
In the above program, we first create a dictionary called `my_dict` with some initial key-value pairs.

We then add a new key-value pair to the dictionary by assigning a value to a new key `"pear"`.

Finally, we print the updated dictionary using the `print()` function.