Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Build a phonebook dictionaryE02. Get a value from a dictionary with a defaultE03. Add and remove dictionary entriesM

Problem No 3

Add and remove dictionary entries

Medium

12 minute session

Summary

Add new entries and remove specified keys from a dictionary, returning an updated dictionary.

Problem statement

Implement update_entries(phonebook, to_add, to_remove) that: - Accepts phonebook: a dictionary (name -> number). - to_add: a dictionary of entries to add or update. - to_remove: an iterable (list/tuple) of keys to remove from the resulting dictionary. Return a new dictionary that starts with the original phonebook order, updates values for existing keys (keeping their original order), appends new keys in the order they appear in to_add, and finally removes any keys listed in to_remove. Do not modify the input phonebook object.

Task

Given an existing dictionary, a mapping of entries to add (or update), and an iterable of keys to remove, return a new dictionary reflecting those changes without mutating the original.

Examples

Add a new key

Input

update_entries({'A': '1'}, {'B': '2'}, [])

Output

{'A': '1', 'B': '2'}

Explanation

B is appended after existing keys.

Input format

A Python expression calling update_entries(phonebook, to_add, to_remove)

Output format

Return the updated dictionary. The test harness compares str(return_value).

Constraints

- phonebook and to_add sizes 0..1000 - to_remove may contain keys not present (ignore them) - Do not mutate the input phonebook - Preserve insertion order rules described above

Samples

Sample input 0

update_entries({'A': '1', 'B': '2'}, {'A': '9', 'C': '3'}, ['B'])

Sample output 0

{'A': '9', 'C': '3'}

Explanation 0

A is updated in place (keeps position), C appended, B removed.

Code editor
Loading editor…

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.

02:09 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.