Lesson guide
What this Python exercise practices
Add and remove set elements is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Practice path
Summary
Practice adding and removing elements from a set and returning the final contents.
Problem statement
Implement a function that starts from an initial collection of items, adds items from a 'to_add' collection and removes items from a 'to_remove' collection. Use a set for these operations. Use discard when removing to avoid errors if an element is not present. Return the final unique items as a sorted list to keep output deterministic.
Task
Perform additions and removals on a set (using add and discard) and return a deterministic, sorted list of the final elements.
Examples
Add an element
Input
([1, 2], [3], [])
Output
[1, 2, 3]
Explanation
Start with {1,2}, add 3 => {1,2,3}, nothing removed; return sorted list.
Input format
Three lists: initial, to_add, to_remove.
Output format
A sorted list with the final unique elements after additions and removals.
Constraints
All items are hashable and of comparable type for sorting. to_add and to_remove may contain duplicates.
Samples
Sample input 0
([1, 2], [3], [])
Sample output 0
[1, 2, 3]
Explanation 0
3 is added to the set.
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 per unlocked preview lesson.