Problem No 20
Find values that appear in exactly one of two sets
Hard≈ 26 minute session
Lesson guide
What this Python exercise practices
Find values that appear in exactly one of two sets is a advanced practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 26 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Advanced
- Estimated time
- 26 minutes
Practice path
Summary
Return the unique values that are present in one collection but not both, presented in sorted order.
Problem statement
Write a function values_in_exactly_one(a, b) that accepts two iterables (like lists or tuples). Determine which values appear in exactly one of the two inputs (i.e., the symmetric difference). Return the result as a sorted list of unique values. Sorting ensures predictable output for testing. You may assume elements within each input are of a type that can be compared with each other (e.g., all ints or all strings).
Task
Given two iterables, compute the values that appear in exactly one of them and return a sorted list of those unique values.
Examples
Simple symmetric difference
Input
values_in_exactly_one([1, 2, 3], [3, 4])
Output
[1, 2, 4]
Explanation
1 and 2 are only in the first; 4 is only in the second.
Input format
Two iterables passed as arguments: values_in_exactly_one(a, b).
Output format
Return a sorted list containing values that appear in exactly one of the inputs.
Constraints
- Elements within each input are comparable (so sorted() works). - Inputs may contain duplicates; the result should contain each unique value once. - Do not use any external libraries.
Samples
Sample input 0
values_in_exactly_one(['a', 'b', 'b'], ['b', 'c'])
Sample output 0
['a', 'c']
Explanation 0
Unique values only in one side are 'a' and 'c', returned sorted.
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.