In Post #64, we were introduced to Python’s dictionary, a powerful data structure for storing data as key-value pairs. Now that we know how to define a dictionary, it’s time to learn how to interact with it.
This post covers the three fundamental operations you’ll perform on dictionaries every day: accessing a value using its key, adding a new key-value pair, and modifying the value of an existing key.
For our examples, we will start with this simple dictionary:
user_profile = {
"name": "Alice",
"age": 30,
"is_active": True
}
Accessing Values Using Keys
Unlike lists that use an integer index, dictionaries use keys to look up their associated values. The syntax is very similar to list indexing, but you place the key inside the square brackets.
The syntax is: my_dict[key]
# Access the value associated with the "name" key
user_name = user_profile["name"]
print(f"The user's name is: {user_name}")
# Access the value associated with the "age" key
user_age = user_profile["age"]
print(f"The user's age is: {user_age}")
The output will be:
The user's name is: Alice
The user's age is: 30
The KeyError
: What Happens When a Key Doesn’t Exist?
This is a very important behavior to understand. If you try to access a key that is not in the dictionary, your program will crash with a KeyError
.
# This will cause a KeyError because the "city" key does not exist
# location = user_profile["city"]
This is a common issue. In our next post, we’ll learn a safer way to get data from a dictionary that avoids this error.
Adding a New Key-Value Pair
Adding a new item to a dictionary is simple and intuitive. You use the same square bracket notation, but on the left side of an assignment operator (=
).
The syntax is: my_dict[new_key] = new_value
If the key does not already exist in the dictionary, a new key-value pair will be created.
print(f"Original dictionary: {user_profile}")
# Add a new key-value pair for "city"
user_profile["city"] = "New York"
print(f"Updated dictionary: {user_profile}")
The output shows the new pair has been added:
Original dictionary: {'name': 'Alice', 'age': 30, 'is_active': True}
Updated dictionary: {'name': 'Alice', 'age': 30, 'is_active': True, 'city': 'New York'}
Modifying an Existing Value
The beauty of the dictionary syntax is that the same operation is used for both adding and modifying. If the key you are assigning a value to already exists in the dictionary, Python doesn’t create a new entry; it simply updates the value associated with that existing key.
The syntax is: my_dict[existing_key] = new_value
print(f"User's age before update: {user_profile['age']}")
# Update the value for the 'age' key
user_profile["age"] = 31
print(f"User's age after update: {user_profile['age']}")
print(f"The full profile is now: {user_profile}")
The output shows the value for the age
key has been replaced:
User's age before update: 30
User's age after update: 31
The full profile is now: {'name': 'Alice', 'age': 31, 'is_active': True, 'city': 'New York'}
This demonstrates that dictionaries, like lists, are a mutable data type. You can change their contents after they have been created.
What’s Next?
You now know the basic mechanics of working with a dictionary: using [key]
to access data, and using [key] = value
to either add a new pair or update an existing one. These are the core operations you will build upon.
As we saw, trying to access a non-existent key with square brackets causes a KeyError
, which can crash your program. How can we handle cases where a key might be missing without causing an error? In Post #66, we will learn about a safer and more flexible way to access dictionary data using the .get()
method.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com