The first time I really understood dictionaries, I was trying to find Kojo's phone number on an old Nokia. I had to scroll. One name at a time, down a list of forty contacts, until I hit the K's. By the time I reached him I had already forgotten what I wanted to say. That phone was a list. Useful, but stubborn — it only knew how to walk through things in order.
The contacts app on the phone I use now does something different. I type "Kojo" and the number appears. No scrolling. No counting. The phone goes straight to the entry filed under that name. That shift — from walking through items in order to looking them up by name — is the whole point of a dictionary in Python.
When a list stops being enough
A list is the right tool when your items have an order and you read them in sequence. Five universities you're applying to. Ten GPAs you want to average. A hundred applicants you'll filter by deadline. The order is part of the meaning, and walking through them one by one is exactly what you want.
But sometimes order is not the question. Sometimes the question is: "What's Kojo's number?" or "What's the capital of Senegal?" or "What GPA did Amara finish with?" In those moments you don't want to start at the top and walk down. You want to point at a name and get the answer back.
That's the shape a dictionary gives you. Each entry has two parts — a key (the label you'll look it up by) and a value (the thing stored under that label). Like a phone book: the name is the key, the number is the value.
Making one
You build a dictionary with curly braces. Each entry is a key: value pair, separated by commas. Here's a small one mapping students to their final GPAs:
gpas = {
"Amara": 3.7,
"Kojo": 3.4,
"Nia": 3.9,
}Read that aloud and it almost sounds like English. Amara to three-point-seven. Kojo to three-point-four. The colon is the link between a label and what's filed under it. The curly braces are just the box that holds the whole collection.
Another shape you'll see all the time — countries to their capitals:
capitals = {
"Ghana": "Accra",
"Senegal": "Dakar",
"Kenya": "Nairobi",
}Same idea. The keys are strings. The values are strings. Nothing fancy underneath.
Looking things up
To pull a value out, you put the key in square brackets:
print(gpas["Amara"]) # 3.7
print(capitals["Ghana"]) # AccraThink of it like handing a cloakroom ticket to the attendant. You give them the key, they bring back what you stored under it. Quick, exact, no scrolling.
There's one thing to watch for. If you ask for a key the dictionary doesn't have, Python doesn't shrug — it raises a KeyError and stops the program:
gpas["Yaw"] # KeyError: 'Yaw'Yaw isn't in there. The program didn't know what to do, so it complained loudly. There's a politer way to ask, using .get():
gpas.get("Yaw") # None
gpas.get("Yaw", "unknown") # "unknown".get() is the version that says "if you have it, hand it over; if not, here's what I'll accept instead." Useful when a missing key is normal, not a bug.
Where dictionaries really shine
The small examples above are useful, but they undersell the idea. The place a dictionary earns its keep is when one thing has many facts about it — a name, a country, a GPA, an email — and you want each fact filed under a clear label.
student = {
"name": "Amara",
"country": "Ghana",
"gpa": 3.7,
}
print(student["name"], "from", student["country"])Notice the shape. This single dictionary is one record. One student. One row of a spreadsheet, if you imagine a spreadsheet with columns called name, country, gpa. That's not a coincidence — most real data you'll touch as a programmer arrives in this shape. Rows from a CSV file. Responses from an API. Each record is a little bundle of named facts, and a dictionary is the natural way to hold it.
Once you can see one record as a dictionary, the world opens up. A class is a list of those dictionaries. A semester's results is a list of those dictionaries. An entire university directory is a list of those dictionaries. Same shape, different scale.
A quick word on tuples
While we're here — Python has one more small container worth mentioning: the tuple. It looks like a list but uses parentheses, and once you make it, you can't change it.
location = (5.6037, -0.1870) # Accra: latitude, longitudeThe classic example is coordinates on a map. Latitude and longitude come as a pair, in that order, and you'd never want to "add a third number" to a coordinate or rearrange it — that would mean something different. The fixed shape is the point. Reach for a tuple when you have a small, fixed group of values that belong together and shouldn't drift. For most other work, lists and dictionaries will carry you.
Take this with you
A list answers "what's next?" A dictionary answers "what's stored under this name?" Once you can hear the difference in a problem — am I walking through things in order, or am I looking one thing up by label — picking the right tool stops feeling like guesswork. Build a few small dictionaries this week. A few capitals. A few GPAs. A contact or two. The shape will start to feel ordinary, and then, quietly, it will start to feel obvious.