Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Add and remove set elements

Practice adding and removing elements from a set and returning the final contents.

Python practice10 minDictionaries & SetsBeginnerLast updated March 18, 2026

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]

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 1

Input

([1, 2], [3], [])

Output

[1, 2, 3]

3 is added to the set.