thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python 3: Lists / 05.05

Practice set operations

Not started

Compute the four original set operations and inspect uniqueness separately from original list order.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob compares sets containing 1 through 4 and 3 through 6, then shows their union and shared 3 and 4.
Rob’s comic strip · Swap values and compare sets

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.

Expected output
[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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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

ONE STEP FORWARD

A set can remove duplicates, but it cannot preserve the original list’s repeated positions. Keep the original list when those positions still matter.