Introduction
Adding a key-value pair to a dictionary is a common task in Python programming. A dictionary is a mutable data structure, meaning that you can modify it by adding or removing key-value pairs as needed. This tutorial will guide you through creating a Python program that adds a new key-value pair to an existing dictionary.
Example:
- Input Dictionary:
{'name': 'John', 'age': 25} - Key to Add:
'city' - Value to Add:
'New York' - Output Dictionary:
{'name': 'John', 'age': 25, 'city': 'New York'}
Problem Statement
Create a Python program that:
- Takes an existing dictionary.
- Adds a new key-value pair to the dictionary.
- Displays the updated dictionary.
Solution Steps
- Create a Dictionary: Initialize a dictionary with some key-value pairs.
- Add a New Key-Value Pair: Use the assignment operation to add a new key-value pair to the dictionary.
- Display the Updated Dictionary: Use the
print()function to display the updated dictionary.
Python Program
# Python Program to Add a Key-Value Pair to a Dictionary
# Author: https://www.rameshfadatare.com/
# Step 1: Create a dictionary with key-value pairs
my_dict = {
'name': 'John',
'age': 25
}
# Step 2: Take key and value input from the user
new_key = input("Enter the key to add: ")
new_value = input("Enter the value for the key: ")
# Step 3: Add the new key-value pair to the dictionary
my_dict[new_key] = new_value
# Step 4: Display the updated dictionary
print("The updated dictionary is:", my_dict)
Explanation
Step 1: Create a Dictionary with Key-Value Pairs
- A dictionary
my_dictis created with some initial key-value pairs, such as'name': 'John'and'age': 25.
Step 2: Take Key and Value Input from the User
- The
input()function prompts the user to enter a new key and value. These inputs are stored in the variablesnew_keyandnew_value, respectively.
Step 3: Add the New Key-Value Pair to the Dictionary
- The new key-value pair is added to the dictionary using the assignment operation
my_dict[new_key] = new_value. This creates a new entry in the dictionary or updates the value if the key already exists.
Step 4: Display the Updated Dictionary
- The
print()function is used to display the dictionary after the new key-value pair has been added.
Output Example
Example 1:
Enter the key to add: city
Enter the value for the key: New York
The updated dictionary is: {'name': 'John', 'age': 25, 'city': 'New York'}
Example 2:
Enter the key to add: country
Enter the value for the key: USA
The updated dictionary is: {'name': 'John', 'age': 25, 'country': 'USA'}
Example 3:
Enter the key to add: profession
Enter the value for the key: Engineer
The updated dictionary is: {'name': 'John', 'age': 25, 'profession': 'Engineer'}
Conclusion
This Python program demonstrates how to add a new key-value pair to an existing dictionary. Understanding how to modify dictionaries is essential for efficiently managing and updating data in Python programs. The ability to dynamically add or update key-value pairs is particularly useful in scenarios where data is continuously changing or being expanded.