The first time I tried to store my classmates’ marks in Python, I used a list: [85, 92, 78]. It worked — until I had to answer one simple question: whose mark is 92? The list knew the numbers but had forgotten the names. That is the exact problem a Python dictionary solves. Think of it like the contacts app on your phone: you type a name, you get a number. Let’s understand dictionaries step by step, with a real example.
📅 Want a study plan for this topic?
Get a free 7-day learning plan in your calendar — one small lesson every day.
What Is a Python Dictionary, Really?
A dictionary (called a dict in Python) is a collection of key-value pairs — every value has a label, called a key, and you look things up by that label instead of by position. You write one with curly braces: {"Ali": 85, "Sara": 92}. Here "Ali" is the key and 85 is the value. Keys must be unique and unchangeable (text and numbers work; lists do not), while values can be anything — numbers, text, even another dictionary. Under the hood, Python uses a hash table, which is why looking up a key is almost instant, even in a dictionary with a million entries. When I first tried this, I kept putting the value first and the key second, and Python just stared back at me with an error. Honestly, the moment it clicked — key first, like a word in a real dictionary — I stopped fighting lists for jobs they were never meant to do. In my opinion, dictionaries are the most useful Python data structure after lists, and every beginner should learn them in the first month.
The Main Idea
Most beginners get stuck right here: they store everything in lists and then write loops to search for one item. A list answers "what is at position 2?" A dictionary answers "what is Sara's mark?" — a much more natural question. The label is the whole point. If you can name the thing you are looking for, a dictionary will find it in one step, no loop needed.
In Simple Words
Here is the whole idea, in five steps:
- Create one with curly braces: marks = {"Ali": 85, "Sara": 92}.
- Add a pair with square brackets: marks["Bilal"] = 78.
- Look up a value by its key: marks["Sara"] gives you 92.
- Change a value the same way: marks["Ali"] = 90.
- Loop over everything with .items(): for name, mark in marks.items().
Let's Solve an Example: Find the Class Topper
Meet Ayesha, a class monitor with four students' marks. She needs the topper's name, then she adds a new student and fixes one mark. Watch the dictionary change, step by step:
| Step | Code | Dictionary State | Result |
|---|---|---|---|
| 1. Start | marks = {"Ali": 85, "Sara": 92, "Bilal": 78} | Ali 85, Sara 92, Bilal 78 | Three students stored |
| 2. Find topper | topper = max(marks, key=marks.get) | Unchanged | "Sara", 92 — the highest value wins |
| 3. Add a student | marks["Usman"] = 95 | Ali 85, Sara 92, Bilal 78, Usman 95 | Four students now |
| 4. Fix a mark | marks["Ali"] = 90 | Ali 90, Sara 92, Bilal 78, Usman 95 | Ali's old 85 is replaced |
| 5. New topper | max(marks, key=marks.get) | Unchanged | "Usman", 95 |
Here is the same example as real code you can run:
marks = {"Ali": 85, "Sara": 92, "Bilal": 78}
topper = max(marks, key=marks.get)
print(topper, marks[topper]) # Sara 92
marks["Usman"] = 95
marks["Ali"] = 90
print(marks.get("Zain", "Not found")) # Not found
Notice step 2, because this is the part most people miss: max() with key=marks.get compares the VALUES but returns the KEY — the name, not the number. That one line replaces a whole loop. And step 5 shows the same trick after an update: Usman takes the crown with 95. The last line shows the safe way to look up a missing key — .get() returns "Not found" instead of crashing. That small habit saves beginners hours of debugging, and it is the same data structures thinking that makes the difference between code that works and code that survives real data.


Dictionaries vs Lists: When to Use Which
| List | Dictionary | |
|---|---|---|
| Looks like | [85, 92, 78] | {"Ali": 85, "Sara": 92} |
| You find things by | Position (index 0, 1, 2) | Label (the key) |
| Best when | Order matters, items are similar | Each item has a name or ID |
| Lookup speed | Slow for big lists (scan one by one) | Almost instant, any size |
| Example job | Daily temperatures for a week | Student marks, user profiles, word counts |
A list is a queue; a dictionary is a labelled drawer. When I need "the third item", I reach for a list. When I need "Sara's mark", I reach for a dictionary — and that choice is half of good programming. The same idea shows up everywhere: Python strings become word counters with a dictionary, and Python files become settings stores when you save dictionaries as JSON. I recommend learning both structures side by side — most real programs use them together.
3 Mistakes Every Beginner Makes
I made all three, so you do not have to. First, using a list as a key: marks[[1, 2]] = 5 crashes with "unhashable type" — keys must be unchangeable, so use a tuple or a string instead. Second, looking up a missing key with square brackets: marks["Zain"] raises a KeyError and stops your program; use marks.get("Zain", "Not found") and it returns a friendly default instead. Third, duplicate keys: {"Ali": 85, "Ali": 90} quietly keeps only 90 — Python does not warn you, the second value just overwrites the first. Honestly, the .get() habit alone is worth the whole article: it turns crashes into calm default values.
Test Your Understanding
Quiz: What does {"a": 1, "b": 2}["b"] return?
- "b"
- 2
- An error
- None
Answer: B. Square brackets with a key return the VALUE stored under that key — here the key "b" holds the value 2.
Conclusion
A Python dictionary is a set of labelled drawers: each key opens its value in one step, no searching needed. Ayesha found her topper, added Usman, and fixed Ali's mark with four short lines — work that would take a loop and a headache with plain lists. So try it right now: open Python, build a dictionary of your five friends' favourite foods, and look one up by name. That tiny moment, when the name hands you the answer instantly, is the whole superpower.
📅 Want a study plan for this topic?
Get a free 7-day learning plan in your calendar — one small lesson every day.


Leave a Reply