The dragon explains
A dict is a stat sheet
The dragon: You know lists — a row of things, looked up by POSITION. A dict is different: it looks things up by NAME.
The dragon: dragon = {"name": "Ember", "hp": 40} — a dict, with two keys: "name" and "hp". Each key holds one value.
example.pyPython 3dragon = {"name": "Ember", "hp": 40}The dragon: dragon["hp"] gets the value stored under the "hp" key. Not "the second thing" — the thing NAMED hp.
example.pyPython 3print(dragon["hp"])Output40The dragon: That's the whole idea: a dict is a stat sheet where every value has a name, so you never have to remember which position it's in.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
dragon = {"name": "Ember", "hp": 40}. What does dragon["name"] give you?
question.pyPython 3dragon = {"name": "Ember", "hp": 40}print(dragon["name"])
- EmberCorrect
- "name" — the key itself
- 40 — the first value in the dict
Why: Ember. dragon["name"] looks up the value stored under the "name" key — by NAME, which is the whole point of a dict.
Question 2 of 2
party = ["Ember", "Blaze"] is a LIST. dragon = {"name": "Ember"} is a DICT. How do you get the first item from each?
- party[0] for the list, dragon["name"] for the dictCorrect
- party[0] and dragon[0] — both use position
Why: party[0] for the list (position-based), dragon["name"] for the dict (name-based) — two different lookup systems for two different tools.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
dragon = {"name": "Ember", "hp": 40}. Print dragon["hp"] — the value stored under the "hp" key, looked up by name, not by position.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.