Dictionaries: key-value pairs
6 minutes read
Beginner friendly
Includes practice
Ever used a real-world dictionary or a phone contact list?
You look up a word (or a name) and instantly get its meaning (or number).
Python’s dictionary works the same way: one side is the key, the other the value.
Runtic's Learning Tips
Struggling at first is normal! Every programmer once typed
KeyErrorfor the first time.Focus on why a step works, not just what to type; your brain forms stronger links that way.
Small daily practice beats a weekend marathon. Ten minutes of “dict” tinkering today builds tomorrow’s confidence.
What is a Dictionary?
A dictionary (dict) is an unordered collection that stores data as pairs:
key ➔ value
Example (a tiny fruit shop stock):
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
- Keys must be unique and immutable (strings, numbers, tuples).
- Values can be anything (numbers, lists, even other dictionaries).
Creating Your First Dictionary
Inline (literal) method:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Or start empty then add:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Try it yourself below.
Practice Challenge
Create a dictionary named book_priceswith three titles as keys and their prices as values. Then, store the number of items in the dictionary inside len_resultusing the len()function, and print it to confirm.
Create a dictionary with exactly three key–value pairs, then use len(...) to get its size.
Accessing Values by Key
Get a value using square brackets:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Safer: dict.get(key, default) avoids crashes if the key is missing.
Adding or Updating Entries
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Removing Entries
del stock["orange"] removes that pair.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
stock.pop("apple", 0) removes and returns the value (0 is fallback).
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Does This Key Exist?
"pear" in stock ➔ True or False
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Useful to prevent errors before accessing.
Looping Through a Dictionary
To go through every key–value pair in a dictionary, use a for loop with two variables: one for the key and one for the value.
This lets you access both parts of each entry inside the loop.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
.keys()– iterate keys only..values()– iterate values only.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Check-in
Quick Practice
3 minutesChoose the best answer for each mini-quiz!
Which statement is correct?
`stock.get('mango', 0)` returns ___ if 'mango' is not present.
How would you remove key 'Alice' from dict `grades`?
Practice: Retrieve a Value
Practice Challenge
Use the given dictionary stockto get the number of bananas and store it in banana_qty.Then, print the result to verify.
Access the value of "banana" using square brackets or .get().
Quick Summary
Dictionaries map unique, immutable keys to values of any type.
Common actions: create, access, add/update, delete, test membership, iterate.
.get()and theinkeyword keep your code safe from missing-key errors.
- You built, queried, and modified Python dictionaries with confidence.
- You practiced safe key access and removal to avoid runtime errors.
- You linked everyday analogies (phonebook) to solid code understanding.
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.