Summary
Create a dictionary from a list of name-number pairs. Handle duplicates by keeping the latest value.
Problem statement
Write a function build_phonebook(pairs) that takes an iterable (e.g., list or tuple) of 2-element tuples or lists. Each inner element contains a name (string) and a phone number (string or number). The function should return a dictionary mapping each name to its latest phone number. If a name appears multiple times, the value should reflect the last occurrence in the input. Maintain insertion order based on the first time a key appears (updating a value for an existing key should not change its position).
Task
Given an iterable of 2-tuples (name, number), return a dictionary mapping each name to its number.
Examples
Basic phonebook
Input
build_phonebook([('Alice', '123'), ('Bob', '456')])
Output
{'Alice': '123', 'Bob': '456'}
Explanation
Creates a dictionary with keys 'Alice' and 'Bob' mapped to their numbers.
Input format
A Python expression calling build_phonebook with an iterable of pairs, e.g. build_phonebook([('Name', 'Number')])
Output format
Return a dict mapping names to numbers. The test harness compares str(return_value).
Constraints
- pairs length can be 0..1000 - Names are hashable (typically strings) - Phone numbers can be strings or numbers - Do not print; return the dictionary
Samples
Sample input 0
build_phonebook([('Zoe', '999')])
Sample output 0
{'Zoe': '999'}
Explanation 0
Single entry results in single-key dict.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.