Python · Python Collections

Sets — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1Creating SetsEasy

Create colors = {"red", "green", "blue"} and print it along with its type().

Q2UniquenessEasy

Create numbers = {1, 2, 2, 3, 3, 3, 4} and print it. What happened to the duplicates?

Q3UniquenessEasy

Given numbers = [1, 2, 2, 3, 3, 3, 4] (a list), convert it to a set to remove duplicates, then print the result.

Q4add()Easy

Start with fruits = {"apple", "banana"}. Add "cherry" using .add() and print the set.

Q5remove() / discard()Easy

Start with fruits = {"apple", "banana", "cherry"}. Remove "banana" using .remove(), then try to .discard() an item that doesn't exist ("mango") without it raising an error.

Q6len()Easy

Print the number of items in letters = {"a", "b", "c", "a", "b"}.

Q7MembershipEasy

Given allowed = {"admin", "editor", "viewer"}, print whether "editor" and "guest" are each in the set.

Q8forEasy

Print each item of numbers = {10, 20, 30} using a for loop.

Q9Empty SetsEasy

Create an empty set correctly (not with {}, which creates a dict), add three items to it, and print it.

Q10OrderingEasy

Create numbers = {5, 1, 3, 2, 4} and print it. Explain in a comment why the printed order might not match insertion order.

Q11UnionMedium

Given set1 = {1, 2, 3} and set2 = {3, 4, 5}, print their union using both the | operator and .union().

Q12IntersectionMedium

Using the same set1 and set2, print their intersection using both & and .intersection().

Q13DifferenceMedium

Using the same set1 and set2, print set1 - set2 and set2 - set1, and explain why they differ.

Q14Symmetric DifferenceMedium

Using the same set1 and set2, print their symmetric difference using both ^ and .symmetric_difference().

Q15Subset / SupersetMedium

Given small = {1, 2} and big = {1, 2, 3, 4}, print small.issubset(big) and big.issuperset(small).

Q16copy() / clear()Medium

Given original = {1, 2, 3}, create copy = original.copy(), add an item to original, and show copy is unaffected. Then .clear() the original.

Q17Update MethodsMedium

Given set1 = {1, 2, 3} and set2 = {3, 4, 5}, use .update() to merge set2's items into set1 in place, and print the result.

Q18pop()Medium

Given numbers = {10, 20, 30}, call .pop() once and print both the removed item and the remaining set. Explain in a comment why you can't predict which item gets removed.

Q19isdisjoint()Medium

Given set1 = {1, 2, 3} and set2 = {4, 5, 6}, print whether they're disjoint (share no elements) using .isdisjoint().

Q20Set from StringMedium

Given word = "mississippi", print the set of its unique characters, and how many unique characters it has.

Q21Real-WorldMedium

Given alice_friends = {"Bob", "Carol", "Dave"} and bob_friends = {"Carol", "Eve", "Frank"}, print their mutual friends.

Q22Real-WorldMedium

Given monday_visitors = {"u1", "u2", "u3"} and tuesday_visitors = {"u2", "u3", "u4"}, print visitors who came on EITHER day, and visitors who came on BOTH days.

Q23Real-WorldMedium

Given required_skills = {"python", "sql", "git"} and candidate_skills = {"python", "git", "excel"}, print which required skills the candidate is MISSING.

Q24Real-WorldMedium

Given a list tags = ["python", "web", "python", "backend", "web", "api"], print the unique set of tags and how many unique tags there are.

Q25Real-WorldMedium

Given team_a = {"Alice", "Bob"} and team_b = {"Bob", "Carol"}, print whether the two teams share ANY member using .isdisjoint(), phrased as a friendly boolean has_overlap.

Q26Real-WorldMedium

Given two lists of email addresses, list1 and list2, print the emails that appear in BOTH lists using sets, then print them as a sorted list.

Q27SubsetMedium

Given cart_items = {"bread", "milk"} and available_items = {"bread", "milk", "eggs", "butter"}, print whether the whole cart can be fulfilled using .issubset().

Q28Real-WorldMedium

Ask the user to enter 5 words one at a time, storing them in a set, then print the number of DISTINCT words entered.

Q29DebuggingMedium

The following code crashes with a TypeError. Find and fix the bug.

tags = {"python", "web", ["backend"]}
print(tags)
Q30FormattingMedium

Given scores = {88, 92, 75, 92, 88, 100}, print the sorted unique scores as a comma-separated string.

Q31ApplicationsHard

Given two sentences, use sets built from .split() to find the words that appear in BOTH sentences (ignoring case).

Q32ApplicationsHard

Given text = "the quick brown fox jumps over the lazy dog", print how many words appear EXACTLY once, by checking each distinct word's .count() in the full split word list.

Q33frozensetHard

Create a frozenset from {1, 2, 3}, demonstrate that it supports normal set operations like &, but raises an error if you try .add() to it.

Q34AlgorithmicHard

Given numbers = [4, 3, 2, 7, 8, 2, 3, 1] where every number appears twice except one, find that single unique number using symmetric_difference chained in a loop (not by counting).

Q35ApplicationsHard

Given set_a = {1, 2, 3}, set_b = {2, 3, 4}, and set_c = {3, 4, 5}, find the elements common to ALL THREE sets, chaining &.

Q36SubsetHard

Given permissions_needed = {"read", "write"} and a list of user permission sets, find which users (by index) can perform the required action, using .issubset() in a loop.

Q37PerformanceHard

Given a list of 100,000 numbers, demonstrate why checking membership with in on a set is dramatically faster than on the equivalent list, explaining the underlying reason in a comment.

Q38ApplicationsHard

Given today_attendance = {"Alice", "Bob", "Carol"} and yesterday_attendance = {"Bob", "Carol", "David"}, print students who attended ONLY ONE of the two days (not both).

Q39ApplicationsHard

Given matrix_rows = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}] (each representing a row's distinct values), find the value common to ALL rows using a loop that intersects them one by one.

Q40DebuggingHard

The following code is supposed to find items unique to set1 only, but the operator is backwards, producing the wrong answer. Find and fix the bug.

set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "cherry", "date"}
 
unique_to_set1 = set2 - set1
print(unique_to_set1)
Q41Mini-ProjectMini-Project

Build a "Common Interests Finder". Ask two users to each enter 5 hobbies (comma-separated in one line, split manually), then print their shared and unique hobbies.

Q42Mini-ProjectMini-Project

Build a "Plagiarism Overlap Checker". Given two texts, compute what percentage of the first text's unique words also appear in the second text.

Q43Mini-ProjectMini-Project

Build a "Tag-Based Content Filter". Given a list of articles (each a (title, tag_set) tuple), print titles of articles that have AT LEAST ONE tag from a user's interested_tags set.

Q44Mini-ProjectMini-Project

Build an "Attendance Overlap Tracker". Given attendance sets for 3 days, print students who attended EVERY day, and students who attended at least one day but not all.

Q45Mini-ProjectMini-Project

Build a "Unique Visitor Counter". Ask the user to enter visitor IDs one at a time until they type "done", then print the total entries made and the number of DISTINCT visitors.

Q46InterviewInterview

Explain why sets require their elements to be hashable, connecting it back to why tuples (but not lists) can go inside a set — demonstrate with both a working and a failing example.

Q47InterviewInterview

Explain when to choose a set over a list or tuple, with one code example demonstrating each collection being the "right" choice for its own use case.

Q48InterviewInterview

Demonstrate that converting a list to a set to remove duplicates does NOT preserve the original order, then show the fix (from the Lists module) that does preserve order.

Q49InterviewInterview

A classic interview puzzle: "given two lists, find elements that are in exactly one of them, not both." Solve it using set operations and explain which operator does this in one step.

Q50CapstoneInterview

Build a "Set Mastery Report" — the most complete demonstration in this module. Given team_python = {"Alice", "Bob", "Carol", "Dave"} and team_java = {"Carol", "Dave", "Eve", "Frank"}, print: members in either team, members in both teams, members exclusive to each team, members in exactly one team, whether team_python is a subset of the combined team, and the total distinct member count.

Still stuck on something?

Book a free 1-on-1 session and we'll work through it together.

Book a Free Session