› Forums › AI & Machine Learning › Understanding .add() in the CS50 Degrees Project: How Multiple IDs Are Stored for the Same Name
- This topic is empty.
-
AuthorPosts
-
June 19, 2026 at 10:24 am #6917
A beginner studying the CS50 AI Degrees project often encounters code like this:
if row["name"].lower() not in names: names[row["name"].lower()] = {row["id"]} else: names[row["name"].lower()].add(row["id"])At first glance, the last line can be confusing:
names[row["name"].lower()].add(row["id"])Many learners naturally wonder:
Does this line create a new dictionary entry, or does it add a new ID to an existing name?
Let’s break it down carefully.
1. What Is the Purpose of the
namesDictionary?The
namesdictionary is designed like this:person name → set of person IDsFor example:
names = { "kevin bacon": {"102"} }This means:
"kevin bacon" has person ID 102
2. When a Name Appears for the First Time
Suppose the CSV row is:
row = { "id": "102", "name": "Kevin Bacon" }Initially:
names = {}The condition:
if row["name"].lower() not in names:becomes:
if "kevin bacon" not in names:which is True.
Therefore Python executes:
names["kevin bacon"] = {"102"}The dictionary now becomes:
names = { "kevin bacon": {"102"} }Notice that the value is a set containing one ID.
3. When the Same Name Appears Again
Now suppose another CSV row contains:
row = { "id": "205", "name": "Kevin Bacon" }The condition:
if "kevin bacon" not in names:is now False because the name already exists.
So Python moves to:
else: names[row["name"].lower()].add(row["id"])which becomes:
names["kevin bacon"].add("205")
4. What Does
.add()Actually Do?The
.add()method belongs to Python sets.It adds a new element into an existing set.
Before:
{"102"}After:
.add("205")the set becomes:
{"102", "205"}The dictionary now looks like:
names = { "kevin bacon": {"102", "205"} }
5. Does
.add()Create a New Dictionary Entry?No.
This is a very important distinction.
The code:
names["kevin bacon"].add("205")does NOT create another key called:
"kevin bacon"Instead, it modifies the existing set already stored under that key.
Conceptually:
existing_set = names["kevin bacon"] existing_set.add("205")The key stays the same.
Only the contents of the set change.
6. Visualizing What Happens
Before
.add():names │ └── "kevin bacon" │ ▼ {"102"}Python executes:
names["kevin bacon"].add("205")Afterward:
names │ └── "kevin bacon" │ ▼ {"102", "205"}The dictionary still has only one key:
"kevin bacon"but the associated set now contains two IDs.
7. Why Use a Set Instead of a Single ID?
Suppose the code used:
names["kevin bacon"] = "102"Later:
names["kevin bacon"] = "205"The second assignment would overwrite the first one.
Result:
{ "kevin bacon": "205" }The original ID “102” would be lost.
Using a set prevents that problem.
8. The Full Flow
First Kevin Bacon row encountered ↓ Create new set {"102"} ↓ Store under key "kevin bacon" ↓ Second Kevin Bacon row encountered ↓ Find existing set {"102"} ↓ Add "205" using .add() ↓ Set becomes {"102", "205"}
9. Why This Matters in the Degrees Project
The CS50 Degrees project must handle cases where multiple people share the same name.
For example:
ID Name
102 Kevin Bacon
205 Kevin BaconThe
namesdictionary therefore stores:{ "kevin bacon": {"102", "205"} }Later, when the user searches for “Kevin Bacon”, the program can detect multiple matching IDs and ask:
Which Kevin Bacon did you mean?
This is why the line:
names[row["name"].lower()].add(row["id"])is so important—it preserves all matching IDs instead of overwriting earlier ones.
-
AuthorPosts
- You must be logged in to reply to this topic.
