› Forums › Python › π From Dictionary of Lists to Dictionary of Objects: Phone Book Explained (Python MOOC 2025) – revised
- This topic is empty.
-
AuthorPosts
-
February 5, 2026 at 11:02 pm #5992
In the early stages of the Python MOOC course, learners build a simple phone book using only basic data structures.
The internal data structure looks like this:
all_persons = {} # Example entries all_persons["Eric"] = ["02-123456", "045-4356713"] all_persons["Emily"] = ["040-324344"]Here:
- Keys β Person names
- Values β Lists of phone numbers
This design is called a dictionary of lists.
It works well for small programs. However, in Part 10 of the course, new requirements are introduced, such as storing addresses and improving maintainability. At this stage, this structure becomes difficult to manage.
To solve this, the course introduces objects inside dictionaries.
π Official course page:
https://programming-25.mooc.fi/part-10/4-application-development/
β Q1: Why is a dictionary of lists no longer enough?
Answer:
A dictionary of lists stores only raw data. When new features are added, the data becomes scattered and difficult to manage.
Problems include:
- No clear structure
- Difficult extension
- More chances of bugs
- Poor readability
Objects allow related data and behavior to be grouped together.
β Q2: What does βdictionary of objectsβ mean?
Answer:
It means that instead of storing lists as values, the dictionary stores objects.
Before
name β list of numbersAfter
name β Person objectExample:
all_persons["Eric"] = Person("Eric")Each
Personobject manages its own data.
β Q3: Why do we need a
Personclass?Answer:
A
Personclass represents one real person in the phone book.It is responsible for:
- Storing the name
- Managing phone numbers
- Storing the address
This follows the single-responsibility principle.
β Q4: What is the basic
Personclass?Answer:
class Person: def __init__(self, name: str): self.__name = name self.__numbers = [] self.__address = None def name(self): return self.__name def numbers(self): return self.__numbers def address(self): return self.__address def add_number(self, number: str): self.__numbers.append(number) def add_address(self, address: str): self.__address = addressThis class encapsulates personal data.
β Q5: How does the
PhoneBookclass connect withPerson?Answer:
The key connecting line is:
self.__persons[name] = Person(name)This line creates a
Personobject and stores it insidePhoneBook.It establishes the relationship between the two classes.
β Q6: How does the
PhoneBookclass change?Answer:
class PhoneBook: def __init__(self): self.__persons = {} def add_number(self, name, number): if name not in self.__persons: self.__persons[name] = Person(name) self.__persons[name].add_number(number) def add_address(self, name, address): if name not in self.__persons: self.__persons[name] = Person(name) self.__persons[name].add_address(address) def get_entry(self, name): return self.__persons.get(name)It now manages
Personobjects instead of lists.
β Q7: Why is
.get(name)used inget_entry()?Answer:
return self.__persons.get(name)This safely returns:
- A
Personobject if found Noneif not found
It prevents runtime errors and simplifies validation.
β Q8: How does searching work now?
Answer:
The user interface retrieves a
Personobject:person = phonebook.get_entry(name) if person is None: print("number unknown") else: for number in person.numbers(): print(number)The UI no longer works with raw lists.
β Q9: Why does the user interface not change much?
Answer:
Because of separation of concerns.
- UI β user interaction
- PhoneBook β data management
- Person β personal data
Only internal structures change.
β Q10: Why does the course recommend βbaby stepsβ in refactoring?
Answer:
Structural changes affect many parts of a program. Changing everything at once often leads to hidden bugs.
The safe approach is:
- Implement
Person - Test
- Update
PhoneBook - Test
- Update UI
This mirrors professional development practice.
β Q11: How is this related to the
ExerciseCounterexample?Answer:
Both examples use the same design pattern:
dictionary β object β methodsExample:
students[name] = ExerciseCounter() students[name].done()Phone book:
all_persons[name] = Person(name) all_persons[name].add_number("123")The idea is identical.
-
AuthorPosts
- You must be logged in to reply to this topic.

