› Forums › Python › CS50’s Introduction to Artificial Intelligence with Python › What is the important difference between lists and sets?
- This topic is empty.
-
AuthorPosts
-
May 22, 2026 at 2:18 pm #6627
A learner working through the CS50 Degrees project noticed this line:
[code language="python"] names[row["name"].lower()] = {row["id"]} [/code]
and wondered:
“Why is the code not using
append()here?”At first glance, it may seem natural to expect something like:
[code language="python"] append(row["id"]) [/code]
However, understanding why append() is not used here helps clarify an important difference between Python lists and sets.
Q1. What is the original code?
[code language="python"] if row["name"].lower() not in names: names[row["name"].lower()] = {row["id"]} [/code]
This code appears inside the load_data() function of the CS50 Degrees project.
Q2. What does this line create?
[code language="python"] {row["id"]} [/code]
creates a Python set containing one value.
Example:
[code language="python"] {"102"} [/code]
So if:
[code language="python"] row["name"] = "Kevin Bacon" row["id"] = "102" [/code]
then Python creates:
[code language="python"] names["kevin bacon"] = {"102"} [/code]
Q3. Why is a set being used?
The project uses sets because multiple actors can share the same name.
Example:
[code] Chris Evans [/code]
could refer to more than one person.
So the structure may eventually become:
[code language="python"] names["chris evans"] = {"123", "456"} [/code]
Sets are useful because they:
- prevent duplicates automatically
- support fast lookup
- allow efficient additions
Q4. Why can’t
append()be used?The learner may expect something like:
[code language="python"] append(row["id"]) [/code]
However, append() only works on lists.
Example:
[code language="python"] mylist = []
mylist.append("102") [/code]
But the CS50 code is not using a list.
It is using a set.
Q5. What method do sets use instead?
Sets use:
[code language="python"] add() [/code]
Example:
[code language="python"] myset = set()
myset.add("102") [/code]
So:
- lists use
append() - sets use
add()
Q6. Why does the first assignment not use
add()either?This line:
[code language="python"] names[row["name"].lower()] = {row["id"]} [/code]
is creating the set for the very first time.
Example:
[code language="python"] names["kevin bacon"] = {"102"} [/code]
At this stage:
- there is no existing set yet
- Python is assigning a brand new set directly
So add() is unnecessary here.
Q7. When does the code actually use
add()?Later, if another actor has the same name, this line runs:
[code language="python"] names[row["name"].lower()].add(row["id"]) [/code]
Suppose initially:
[code language="python"] names["chris evans"] = {"123"} [/code]
Then another actor with the same name appears:
[code language="python"] names["chris evans"].add("456") [/code]
Now the set becomes:
[code language="python"] {"123", "456"} [/code]
Q8. Why are sets better than lists here?
Suppose lists were used instead:
[code language="python"] names["chris evans"] = ["123"] [/code]
Then this could accidentally happen:
[code language="python"] append("123") [/code]
Result:
[code language="python"] ["123", "123"] [/code]
Now duplicates exist.
Sets automatically prevent this problem.
Q9. What is the important difference between lists and sets?
List example:
[code language="python"] ["102", "103"] [/code]
Uses:
[code language="python"] append() [/code]
Set example:
[code language="python"] {"102", "103"} [/code]
Uses:
[code language="python"] add() [/code]
This is one of the most important differences between lists and sets in Python.
Q10. What is the biggest conceptual takeaway?
The code does not use:
[code language="python"] append() [/code]
because the structure is a set, not a list.
Sets are ideal here because they:
- store unique actor IDs
- prevent duplicates automatically
- support efficient graph relationships
This design becomes especially important later when the CS50 Degrees project performs graph traversal and searches actor connections efficiently.
-
AuthorPosts
- You must be logged in to reply to this topic.
