Can you add or modify key-value pairs in a dictionary?

QuestionsQuestions8 SkillsProPython Data StructuresAug, 20 2025
0139

Yes, you can add or modify key-value pairs in a dictionary in Python. Here’s how you can do it:

Adding Key-Value Pairs

You can add a new key-value pair by assigning a value to a new key:

my_dict = {'name': 'Alice', 'age': 25}
my_dict['phone'] = '123-456-7890'  # Adding a new key-value pair
print(my_dict)  # Output: {'name': 'Alice', 'age': 25, 'phone': '123-456-7890'}

Modifying Existing Key-Value Pairs

You can modify the value associated with an existing key by assigning a new value to that key:

my_dict['age'] = 26  # Modifying the value of the existing key 'age'
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'phone': '123-456-7890'}

These operations allow you to manage the contents of a dictionary effectively.

0 Comments

no data
Be the first to share your comment!