› Forums › Python › Python PhoneBook: The Hidden Mistake in return self.__persons[name].append(number)
- This topic is empty.
-
AuthorPosts
-
January 17, 2026 at 1:03 am #5942
✅ Q&A: Understanding
return self.__persons[name].append(number)in a PhoneBook ProgramContext
In this program, a
PhoneBookclass is created to store people’s phone numbers.
Since one person can have multiple phone numbers, the phone book stores each name with a list of numbers.
✅ Initial Code (Original Version)
class PhoneBook: def __init__(self): self.__persons = {} def add_number(self, name: str, number: str): if not name in self.__persons: # add a new dictionary entry with an empty list for the numbers self.__persons[name] = [] self.__persons[name].append(number) def get_numbers(self, name: str): if not name in self.__persons: return None return self.__persons[name]✅ In this version:
add_number()adds phone numbersget_numbers()retrieves the list of numbers for a person
✅ Q&A
Q1. What does this PhoneBook program store?
Answer:
It stores names and phone numbers using a dictionary like this:{ "Ali": ["123", "456"], "Sara": ["999"] }Each key is a name, and the value is a list of numbers.
Q2. Why does the code use an empty list
[]for new names?Answer:
Because the program wants to store multiple numbers per person.So when a person is first added, this happens:
self.__persons[name] = []That creates a space to start adding numbers.
Q3. What does this line do?
self.__persons[name].append(number)Answer:
It adds the phone number into the list for that person.Example:
self.__persons["Ali"].append("123")Now
"Ali"has:["123"]
Q4. What happens if we change
add_number()to this?def add_number(self, name: str, number: str): if not name in self.__persons: self.__persons[name] = [] return self.__persons[name].append(number)Answer:
✅ The number will still be added successfully.
❌ But the function will returnNone.
Q5. Why does it return
None?Answer:
Becauselist.append()in Python always returnsNone.It modifies the list in-place, meaning it updates the existing list instead of creating a new one.
So this always happens:
return ["123"].append("456") # returns None
Q6. Will the phone number still be saved even if it returns
None?Answer:
✅ Yes, the phone number is saved.
But the return value of the function is useless.
Q7. What will be the output in this example?
pb = PhoneBook() result = pb.add_number("Ali", "123") print(result) print(pb.get_numbers("Ali"))Answer:
None ['123']So the number gets stored correctly, but the returned value is
None.
Q8. What is the best correct way to write
add_number()?Answer:
The best way is to not return.append().✅ Recommended:
def add_number(self, name: str, number: str): if name not in self.__persons: self.__persons[name] = [] self.__persons[name].append(number)This keeps the method clean and clear.
Q9. What if we want
add_number()to return something useful?Answer:
You can return the updated list after appending.✅ Useful version:
def add_number(self, name: str, number: str): if name not in self.__persons: self.__persons[name] = [] self.__persons[name].append(number) return self.__persons[name]Now it returns:
['123']
✅ Final Takeaway
ߓ Never do this:
return my_list.append(item)Because it will always return:
✅
Noneߓ Instead:
- Append first
- Return something meaningful only if needed
-
AuthorPosts
- You must be logged in to reply to this topic.

