Introduction

You already know Python lists β€” a neat row of items you grab by position number. But what if you want to grab things by name instead of by number? Like looking up a student’s marks by typing their name, not by remembering they were “item number 3”?

That’s exactly what a Python dictionary does, and honestly, once it clicks, you’ll start seeing dictionaries everywhere β€” in settings files, in API responses, even in JSON data. Let’s understand it step by step, with an 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?

A dictionary (or dict) is a Python data structure that stores data as key–value pairs. Each key is a unique label, and each value is the data attached to that label. You write one with curly braces: {"ali": 85}.

The main idea

Think of a real dictionary: you look up a word (the key) and get its meaning (the value). A Python dictionary works the same way β€” you look up a label and get the data behind it. A list is like a numbered apartment building; a dictionary is like a street where every house has a nameplate.

In simple words

  1. Pick a key β€” a short, unique label like "ali" or "email".
  2. Attach a value to it β€” any data: a number, a string, even another list.
  3. Wrap your pairs in curly braces {}, separated by commas.
  4. Look anything up later with square brackets: grades["ali"] gives you 85.

If lists and loops are still fresh for you, my Python lists and loops guide pairs perfectly with this one β€” dictionaries are the natural next step.

Let's Solve an Example: A Student Gradebook

Let's build a tiny gradebook for three students. Watch how the dictionary grows at each step β€” this is the pattern you'll use in real programs:

# Step 1: Create an empty dictionary
grades = {}
# grades is now: {}

# Step 2: Add students (key = name, value = marks)
grades["ali"] = 85
grades["sara"] = 92
# grades is now: {"ali": 85, "sara": 92}

# Step 3: Update a value (same syntax as adding!)
grades["ali"] = 88
# grades is now: {"ali": 88, "sara": 92}

# Step 4: Look something up
print(grades["sara"])   # Output: 92

# Step 5: Loop through everything
for name, marks in grades.items():
    print(name, "scored", marks)
# Output:
# ali scored 88
# sara scored 92

Notice something neat in Step 3? Adding and updating use the exact same line of code. If the key exists, Python updates it; if not, it creates it. When I first tried this, I kept writing separate "add" and "update" logic like in other languages β€” Python just doesn't make you bother.

Here's the same build-up as a picture, plus a short animation:

Dictionary vs List: When to Use Which

Beginners often ask me which one to pick. Here's my rule of thumb:

QuestionUse a listUse a dictionary
How do you find items?By position: items[0]By label: grades["ali"]
Can two items share an identifier?Yes β€” duplicates allowedNo β€” keys must be unique
Best for...Ordered collections, e.g. a to-do listLookups, e.g. usernames β†’ passwords
Example["buy milk", "call ammi"]{"username": "junaid", "city": "Layyah"}

Honestly? If you catch yourself writing if name == "ali": ... elif name == "sara": ... chains, that's Python tapping you on the shoulder and whispering "use a dictionary."

3 Beginner Traps (and How to Dodge Them)

Most beginners get stuck in exactly three places with dictionaries β€” I know because I hit all three myself:

1. The KeyError ambush. Ask for a key that doesn't exist β€” grades["bilal"] β€” and Python crashes with a KeyError. The safe way: grades.get("bilal", "not found") returns "not found" instead of exploding. I recommend .get() as your default habit.

2. Forgetting keys must be unique. Write {"a": 1, "a": 2} and Python quietly keeps only the last one. No warning, no error β€” just silent overwriting. If your data looks "mysteriously wrong," check for duplicate keys first.

3. Using a list as a key. Keys must be unchangeable (strings, numbers, tuples β€” not lists). {["x"]: 1} throws a TypeError. This one confused me for a whole afternoon in my first month; now it's second nature.

Test Your Understanding

Question: What does this code print?

marks = {"ali": 85}
marks["sara"] = 92
print(marks["ali"])
  1. 92
  2. 85
  3. KeyError
  4. {"ali": 85, "sara": 92}

Answer: B β€” marks["ali"] looks up the value stored under the key "ali", which is still 85.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Conclusion

Lists remember order; dictionaries remember meaning. With just curly braces, keys, and values, you can model real-world things β€” gradebooks, user profiles, settings β€” in a way that reads almost like English. Start with .get() for safe lookups, watch out for duplicate keys, and the next time an API hands you a blob of JSON, you'll realize you've been reading dictionaries all along. If you haven't yet, go back and try the gradebook example yourself β€” typing it beats reading it, every single time.

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading