› Forums › Python › CS50’s Introduction to Artificial Intelligence with Python › Why Some Data Gets Two Dictionaries
- This topic is empty.
-
AuthorPosts
-
April 26, 2026 at 6:17 am #6455
When beginners study the CS50 AI Search project, one common question appears:
Why do we create two dictionaries for people, but not two for movies?
This is an excellent question because it teaches an important programming concept:
Data structures are designed around search needs.
The Three Dictionaries Used
names = {} people = {} movies = {}Each dictionary has a different purpose.
1.
namesDictionaryThis helps when the user types a person’s name.
Example:
"tom hanks" -> {"158"}If multiple people share the same name:
"chris evans" -> {"102", "347"}So this dictionary helps us search quickly by name.
2.
peopleDictionaryThis stores full details using a unique person ID.
Example:
"158" -> { "name": "Tom Hanks", "birth": "1956", "movies": {"112384", "109830"} }Why use ID?
Because IDs are unique, names are not.
3.
moviesDictionaryThis stores movie details using movie ID.
Example:
"112384" -> { "title": "Forrest Gump", "year": "1994", "stars": {"158", "705"} }
Why People Need Two Dictionaries
Because people are searched by name, but names may repeat.
So we need:
Search Index
names["tom hanks"]Detail Storage
people["158"]This is similar to a library:
- Search by author name
- Store record by unique membership number
Why Movies Usually Need Only One Dictionary
In this project, movies are mostly accessed through movie IDs already stored in person records:
people["158"]["movies"]Then:
movies[movie_id]So no second movie-title lookup is necessary.
If Users Searched Movies by Title Often…
Then we could create:
titles = {}Example:
"titanic" -> {"271"}Then movies would also have two dictionaries.
Real-World Tech Analogy
E-commerce Website
You may have:
products_by_idproducts_by_nameproducts_by_sku
Multiple indexes improve speed depending on search need.
Core Programming Lesson
Extra dictionaries are indexes.
Indexes are created where searching happens often or where duplicates exist.
One-Line Summary
People got two dictionaries because names are searchable but not unique. Movies only needed one because IDs were enough for the project workflow.
Beginner Mindset Tip
When designing programs, ask:
What will users search most often?
That answer often determines your data structure.
-
AuthorPosts
- You must be logged in to reply to this topic.
