Algorithms: Dictionaries
What is a dictionary and how do you reach a piece of information by its name (its key) instead of its position? Learn key–value mapping, adding, updating, lookup and traversal — no code.

Series · Algorithms
- 1. What Is an Algorithm? — Programming From Scratch
- 2. Algorithms: Flowcharts
- 3. Algorithms: Pseudocode
- 4. Algorithms: Variables
- 5. Algorithms: Conditionals
- 6. Algorithms: Loops
- 7. Algorithms: Lists
- 8. Algorithms: Functions
- 9. Algorithms: Dictionaries
- 10. Algorithms: Final Steps
In the previous post we learned to define a job once and call it by name again and again — functions. Before that, with lists, we saw how to hold many pieces of information under a single name. But lists had a limit: to reach an element you had to know its position number — grades[3]. What if you don’t want “the third student” but directly “Ada’s grade”? That is what this post is about: a new data structure that lets you reach information by its name rather than its order — the dictionary.
The name is no accident. Think of a real dictionary: you look up a word and find its definition. You don’t say “word 5,842” — you look up the word itself. A dictionary in a computer is exactly that: you give a key (the name you’re looking for) and get back its value (the information).
Why do we need dictionaries?
In the lists post we touched on a problem: keeping a list of students and a list of grades side by side. Names in one list, grades in another; we kept both in the same order so that names[i] and grades[i] matched the same student. We called this parallel lists:
names ← ["Ada", "Can", "Ece"]grades ← [90, 75, 60]
# Ada's grade: which position is she in names? Find it, then look at the same position in grades.This works but is fragile. If you add a name to names and forget to add a grade to grades, you break the alignment; from then on names[3] shows one student and grades[3] shows someone else’s grade. Worse, to get “Ada’s grade” you must first search for Ada in the list to find her position (the one-by-one search from lists), then use that position in the other list. Two lists, two jobs for one piece of information.
But what you really have in mind is a single mapping: each name is tied to a grade. “Ada → 90, Can → 75, Ece → 60.” A dictionary holds this mapping directly, in one structure:
grades ← { "Ada": 90, "Can": 75, "Ece": 60 }
# Ada's grade:PRINT grades["Ada"] → 90Two lists, the alignment worry, the search — all reduced to one line. When you say grades["Ada"], the computer gives you Ada’s grade directly; it doesn’t scan the list from start to end, doesn’t bother with “which position was Ada in?”
What is a dictionary?
A dictionary is a data structure made of linked key–value pairs.
- Key: the name of what you’re looking for — the word in a dictionary, the name in a phone book. Above,
"Ada". - Value: the information tied to that key — the word’s definition, the name’s number. Above,
90.
We write a dictionary with curly braces { }, state each pair as key: value, and separate the pairs with commas. In a list we used square brackets [ ] and order; in a dictionary we use curly braces { } and keys:
book ← { "Ada": "0532...", "Can": "0543...", "Emergency": "112" }
PRINT book["Emergency"] → 112Note: keys don’t have to be text (they can be numbers too), but where they’re most useful is reaching information by a meaningful name. Writing book["Emergency"] is far clearer than writing book[3] and trying to remember “the emergency number was in position three, I think.”
Reaching a value
To get a value from a dictionary we use square brackets, just like a list — but inside we write a key instead of a position number:
grades ← { "Ada": 90, "Can": 75, "Ece": 60 }
PRINT grades["Ada"] → 90PRINT grades["Ece"] → 60You can picture this as a mapping (an arrow carrying you from one side to the other): you give the key, the dictionary hands you back its value.
flowchart LR
A["Key<br/>"Ada""] --> B["grades<br/>DICTIONARY<br/>Ada→90, Can→75, Ece→60"]
B --> C["Value<br/>90"]This resembles the black box from the functions post: you give something and get something back. The difference is that here what you give is a key, and what you get is the value tied to that key.
Adding and updating
Adding a new pair to a dictionary is surprisingly simple: you assign a value to the new key — just like the ← assignment arrow we used for variables:
grades ← { "Ada": 90, "Can": 75 }
grades["Ece"] ← 60 (a new pair: "Ece" → 60)# Now: { "Ada": 90, "Can": 75, "Ece": 60 }And if we assign a value to a key that already exists? Then the old value is updated:
grades["Ada"] ← 95 ("Ada" already existed → her value goes from 90 to 95)# Now: { "Ada": 95, "Can": 75, "Ece": 60 }Is a key there? (checking)
Remember the trap above: asking for a missing key can cause an error. That’s why we often need to ask “is this key in the dictionary?” We do this with conditionals:
IF "Ada" IS A KEY IN grades THEN PRINT "Ada's grade: " + grades["Ada"]ELSE PRINT "Ada is not registered."ENDIFThe expression "Ada" IS A KEY IN grades gives back a true/false (boolean) just like in conditionals: true if the key is in the dictionary, false if not. The “look first, use it if it’s safe” pattern is one of the most common you’ll set up when working with dictionaries.
Looping over a dictionary: for each key
To walk a list from start to end we used a loop: we incremented a counter i from 1 to length(list) and said list[i]. But a dictionary has no position number — there’s no such thing as “the 3rd key.” So how do we walk it?
For this we use a slightly different loop: the “for each” loop. This loop takes the keys in the dictionary one by one and hands them to you; each pass you process that key and its value:
grades ← { "Ada": 90, "Can": 75, "Ece": 60 }
FOR EACH key IN grades PRINT key + ": " + grades[key]ENDFORThis loop prints:
Ada: 90Can: 75Ece: 60Each pass, key becomes “Ada”, “Can”, “Ece” in turn; grades[key] is that key’s value. You can think of it as a sibling of the WHILE … ENDWHILE counted loop from the loops post, adapted to a dictionary: there we advanced the counter ourselves, here the loop brings us each key in turn.
Removing
To take a pair out of a dictionary, we use an operation like the REMOVE from lists — but by key, not by position number:
grades ← { "Ada": 90, "Can": 75, "Ece": 60 }
REMOVE "Can" FROM grades# Now: { "Ada": 90, "Ece": 60 }Here there’s no “the elements behind it shift” worry from lists — because a dictionary had no order to shift in the first place. “Can” is removed, “Ada” and “Ece” stay exactly as they were. A small but pleasant side benefit of the dictionary being position-independent.
A value can be anything: a list inside a dictionary
So far our values have always been a single number or a piece of text. But a key’s value can be a list too. For example, if you want to hold several grades per student:
grades ← { "Ada": [90, 85, 100], "Can": [70, 60, 80] }
PRINT grades["Ada"] → [90, 85, 100] (all of Ada's grades)PRINT grades["Ada"][1] → 90 (Ada's first grade)grades["Ada"] gives you a list; to get its first element you add one more square bracket: grades["Ada"][1]. This way you can apply the average function from functions directly to a student’s grades: average(grades["Ada"]). Small pieces (dictionary, list, function) slot into each other to build bigger jobs — exactly as we promised in the functions post.
A dictionary = a thing’s properties (toward objects)
One use of a dictionary is not to map different things to each other, but to hold the properties of a single thing together. Think of a person: they have a name, an age, a city. Instead of keeping these scattered across parallel variables, you can gather them in one dictionary:
person ← { "name": "Ada", "age": 30, "city": "Istanbul" }
PRINT person["name"] → AdaPRINT person["city"] → IstanbulHere the keys ("name", "age", "city") are the thing’s property names, and the values are the information for those properties. person is now a “portable” information package as a whole: you can hand it to a function on its own, or make it an element of a list (for example people ← [person1, person2, person3] — a list where each element is a dictionary).
When a list, when a dictionary?
Both hold several pieces of information together; but they answer different questions well. A table to make the choice easier:
| Question / situation | List | Dictionary |
|---|---|---|
| Access is by what? | By position (list[3]) | By key (grades["Ada"]) |
| Does order matter? | Yes — element order is meaningful | No — reached by key |
| Good for the question | ”What’s the next element?”, “Walk them all" | "What corresponds to this name/id?” |
| Example | A shopping list, the next steps | name→number, product→price, word→definition |
| Same item twice? | Possible (a value can repeat) | Keys can’t repeat (each key is unique) |
The short rule: if you’ll process the data start to end, in order or the order itself is meaningful, use a list; if you need fast access from a name/id to a piece of information, use a dictionary. And as you see, the two aren’t enemies but friends: a dictionary’s value can be a list, and a list’s element can be a dictionary. Real programs use the two nested together.
Common mistakes
Try it yourself
Pen and paper are enough. For each exercise, draw the dictionary as a little table (one column of keys, one column of values), then trace the steps on it one by one. Remind yourself that you reach a value by its key, not by a position number.
Exercise 1 — A phone book (easy)
Draw the dictionary
book ← { "Ada": "0532", "Can": "0543", "Emergency": "112" }. Then find, in order:book["Emergency"],book["Ada"]. Then addbook["Ece"] ← "0555"and write the final state of the dictionary.
Exercise 2 — There or not? (easy)
For the
bookabove, write a “find number” logic: given a name, if the name is in the book print its number, if not print “Not registered.” Run it for"Can"and"Zeynep".
Exercise 3 — Basket total (medium)
prices ← { "apple": 15, "milk": 40, "bread": 10 }is a product→price dictionary. There’s also a listbasket ← ["apple", "apple", "bread"]. Compute the total price of the products in the basket.
Exercise 4 — Letter counter (medium)
Count how many times each letter appears in the word
"beekeeper"into a dictionary. The result should be a dictionary like:{ "b": 1, "e": 5, "k": 1, "p": 1, "r": 1 }.
Exercise 5 — Student report (mini project)
Build a dictionary mapping each student to their grades:
report ← { "Ada": [90, 85, 100], "Can": [70, 60, 80] }. Then, for each student, print their name and grade average.
Summary
Related posts
Frequently asked questions
What is a dictionary?
A dictionary is a data structure that maps one piece of information to another: each entry has a key (the name you look up) and a value (the information tied to that name). Just like a real dictionary — you look up a word (key) and find its definition (value). A phone book is a dictionary too: name → number. It lets you reach a piece of information by its name rather than by a position number.
What is the difference between a list and a dictionary?
In a list you reach elements by position (in order): list[1], list[2]… In a dictionary you reach elements by a meaningful key: grades["Ada"]. A list answers "what is the third element?" well; a dictionary answers "what is Ada's grade?" well. Use a list for data where order matters and you walk it start to end; use a dictionary when you need fast access from a name to a piece of information.
What are keys and values?
A key is the name of what you are looking for — the word in a dictionary, the name in a phone book. A value is the information tied to that key — the word's definition, the name's number. A dictionary is made of key–value pairs. You give the key (grades["Ada"]) and get back its value (90). In a dictionary every key is unique: the same key cannot appear twice.
How do you add to or update a dictionary?
Both happen with the same simple step: you assign a value to a key. If you say grades["Zeynep"] ← 85, and the key "Zeynep" is not in the dictionary, a new entry is added; if it is already there, its value is replaced with 85 (updated). So in a dictionary, adding and updating are the same operation; the difference is just whether the key is already present.
How do you check whether a key is in a dictionary?
Before reaching a value it is important to ask whether its key exists, because asking for a missing key can cause an error. We do this with a conditional: IF "Ada" IS A KEY IN grades THEN … This gives you the "look first, then use it if present" pattern. This check is at the heart of many algorithms, such as a word counter.
How do you loop over a dictionary?
We used to walk a list by its position number; a dictionary has no position number, so we use a "for-each" loop that visits each key: for each key in grades, we process that key and the value grades[key]. This lets you visit every pair in the dictionary one by one — to sum, to print, to count.
Are a dictionary and an object the same thing?
They are close relatives. With a dictionary you can hold the properties of a single thing: person ← { "name": "Ada", "age": 30, "city": "Istanbul" }. Here the keys are property names and the values are the information for those properties. In real programming, what we call an "object" is often built on exactly this kind of key–value mapping. So understanding a dictionary is the first step toward understanding objects later.
When is a dictionary better than a list?
When you often need to reach a piece of information from a name (or an id). Instead of keeping two parallel lists (names and grades) and trying to keep them aligned, you keep a single dictionary (name → grade); this reduces the risk of bugs and lets you reach something like "Ada's grade" directly. A dictionary also reaches the key you want almost instantly, without scanning a list from start to end.