05.05 · Swap values and compare sets / Practice · 12 min
Compare two sets and remove duplicates
Union and intersection answer different questions about the same two collections.
Activity 5 of 6 in Swap values and compare sets
Python 3: Lists / 05.05
Practice set operations
Not started
Compute the four original set operations and inspect uniqueness separately from original list order.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Your task: four operations
Keep first_set as {1, 2, 3, 4} and second_set as {3, 4, 5, 6}. Assign sorted list results to union_values, shared_values, first_only and different_values, using union, intersection, first-minus-second and symmetric difference respectively.
Print those four named lists in that order. Expect [1, 2, 3, 4, 5, 6], [3, 4], [1, 2] and [1, 2, 5, 6]. Leave both input sets unchanged.
Your task: uniqueness without changing the original
Keep original_numbers as [1, 2, 2, 3, 4, 4, 5]. Convert it to a set, then collect a sorted list in unique_values. Print original_numbers followed by unique_values, which should be [1, 2, 3, 4, 5].
The checks inspect stored sorted results, output and input snapshots. They do not prove the operators used. After passing, try disjoint sets and equal sets; predict how intersection and symmetric difference change. Restore the starting sets for fixed-input checks.
Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)6 steps · Saved work · Instant feedback
YOUR TURN
Compare memberships and preserve the original list
Store sorted union, intersection, first-only and symmetric-difference results. Convert the duplicate list to unique sorted values while preserving the original. Keep supplied input snapshots.
[1, 2, 3, 4, 5, 6] [3, 4] [1, 2] [1, 2, 5, 6] [1, 2, 2, 3, 4, 4, 5] [1, 2, 3, 4, 5]
Keep the starting inputs. Print these lines in this order, without extra output.
- Store the requested union values
- Store the requested shared values
- Store the requested first only
- Store the requested different values
- Store the requested original numbers
- Store the requested unique values
- Store the requested first snapshot
- Store the requested second snapshot
- Output 1 matches the requested result
- Output 2 matches the requested result
- Output 3 matches the requested result
- Output 4 matches the requested result
- Output 5 matches the requested result
- Output 6 matches the requested result
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Translate each membership question
Use | for either set, & for both, - for first but not second, and ^ for exactly one. Wrap each expression with sorted.
Keep the duplicate list intact
set(original_numbers) constructs unique membership; sorted turns that result into a predictable list without changing original_numbers.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
The set expressions answer different membership questions. Sorting their displayed lists makes the output reproducible while leaving both input sets unchanged.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Read the starting values
Identify the original values and what must remain unchanged.
first_set = {1, 2, 3, 4}Expected output for this step
(No output)
Follow the change and compare results
The set expressions answer different membership questions. Sorting their displayed lists makes the output reproducible while leaving both input sets unchanged.
first_set = {1, 2, 3, 4}
second_set = {3, 4, 5, 6}
union_values = sorted(first_set | second_set)
shared_values = sorted(first_set & second_set)
first_only = sorted(first_set - second_set)
different_values = sorted(first_set ^ second_set)
print(union_values)
print(shared_values)
print(first_only)
print(different_values)
original_numbers = [1, 2, 2, 3, 4, 4, 5]
unique_values = sorted(set(original_numbers))
print(original_numbers)
print(unique_values)
# Supplied verification: leave these input snapshots unchanged.
first_snapshot = sorted(first_set)
second_snapshot = sorted(second_set)Expected output for this step
[1, 2, 3, 4, 5, 6] [3, 4] [1, 2] [1, 2, 5, 6] [1, 2, 2, 3, 4, 4, 5] [1, 2, 3, 4, 5]
See the complete solution
first_set = {1, 2, 3, 4}
second_set = {3, 4, 5, 6}
union_values = sorted(first_set | second_set)
shared_values = sorted(first_set & second_set)
first_only = sorted(first_set - second_set)
different_values = sorted(first_set ^ second_set)
print(union_values)
print(shared_values)
print(first_only)
print(different_values)
original_numbers = [1, 2, 2, 3, 4, 4, 5]
unique_values = sorted(set(original_numbers))
print(original_numbers)
print(unique_values)
# Supplied verification: leave these input snapshots unchanged.
first_snapshot = sorted(first_set)
second_snapshot = sorted(second_set)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)Continue learning

7 days freeTry The Code Zone
THE CODE ZONE COMMUNITY
Your next step is easier together.
Try the community free for 7 days.
No charge during your trial. Choose your plan on Skool.
- Ask questions and get support in the community
- Watch every course video walkthrough
- Find a study buddy in the 24/7 study room
Make the most of your trial with Premium.
Choose Premium on Skool to try Thursday live help sessions. Bring your questions, work through code, and get unstuck together.
Scheduled Thursdays in Eastern Time. Check the Skool calendar for your local date and time.
Have a website account? Sign in