Accessing & Changing Dictionary Values
(a.k.a. Editing Your Game Stats Like a Developer With Admin Powers)
You’ve created your first dictionary.
Great.
Now it’s time to reach inside, grab the data you want, and change it like you’re updating a character sheet in a role-playing game.
Dictionaries store information in key–value pairs, which means you access values using their keys, not indexes.
Let’s dive in.
1. Accessing Values (a.k.a. “Show me the stats!”)
Here’s a dictionary:
potion = {
"type": "Health Potion",
"heal": 25,
"rarity": "Uncommon"
}
#To access values, you use the key inside square brackets (Do NOT type this line in the codes):
print(potion["type"])
print(potion["heal"])
print(potion["rarity"])
Clean. Direct. No guessing.
2. Changing Values (a.k.a. Upgrading Your Gear Without Asking Permission)
Let’s say your potion needs a buff.
Just assign a new value to the key:
potion = {
"type": "Health Potion",
"heal": 25,
"rarity": "Uncommon"
}
potion["heal"] = 40
potion["rarity"] = "Rare"
#Now print again (Do NOT type this line in the codes)
print(potion["heal"])
print(potion["rarity"])
Your potion just got a promotion.
3. Adding New Key–Value Pairs (a.k.a. Giving Your Item More Personality)
You can add new information anytime:
potion = {
"type": "Health Potion",
"heal": 25
}
potion["price"] = 15
# Print it:
print(potion["price"])
Your potion now has an economy degree.
4. Removing Keys (a.k.a. Cleaning Up Your Data Closet)
If a stat is no longer needed:potion = {
"type": "Health Potion",
"heal": 40,
"rarity": "Rare"
}
del potion["rarity"]
#Now print the whole dictionary:
print(potion)
Rarity? Never heard of her.
Practice (Your Turn!)
Mini-Challenge #1
Create a dictionary called player with:
- name
- health
- level
Then print each value.
Mini-Challenge #2
Create a dictionary called enemy with:
- name
- health
- attack
Increase the enemy’s attack by 5 and print the new value.


