About These Assignments
This page contains 30+ unique practice assignments covering all set concepts in Python . Unlike the topic-specific pages, these problems are designed to test your ability to choose the right set technique for each situation and combine set operations effectively.
š¢ Beginner
š Intermediate
š“ Advanced
šÆ Unique Challenge: Each problem is designed to be different from the topic-specific assignments. You'll need to decide which set technique (methods, comprehension, unpacking, etc.) is most appropriate for each scenario.
š” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.
1. Set Creation Challenges 4 Questions
šÆ Focus: Create sets using different techniques.
Beginner Q1: Create Set from List
Create a set from a list containing duplicate elements (remove duplicates).
ā¶ Show Solution
š” Solution:
numbers = [1, 2, 3, 2, 4, 1, 5, 3, 6]
unique_set = set(numbers)
print(unique_set) # {1, 2, 3, 4, 5, 6}
š Explanation: set() constructor removes duplicates automatically.
Beginner Q2: Set of Characters
Create a set of unique characters from a string.
ā¶ Show Solution
š” Solution:
text = "hello world"
char_set = set(text)
print(char_set) # {'h', 'e', 'l', 'o', ' ', 'w', 'r', 'd'}
š Explanation: Strings are iterable, set() creates unique characters.
Intermediate Q3: Set of Tuples
Create a set containing tuples of (name, age).
ā¶ Show Solution
š” Solution:
data = {("Alice", 25), ("Bob", 30), ("Charlie", 35)}
print(data) # {('Alice', 25), ('Bob', 30), ('Charlie', 35)}
š Explanation: Sets can contain tuples (hashable) as elements.
Advanced Q4: Set from Range with Condition
Create a set of even numbers from 1 to 20 using set comprehension.
ā¶ Show Solution
š” Solution:
evens = {x for x in range(1, 21) if x % 2 == 0}
print(evens) # {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
š Explanation: Set comprehension with condition.
2. Set Methods Challenges 6 Questions
šÆ Focus: Use built-in set methods effectively.
Beginner Q5: Add and Remove
Add an element to a set, then remove it using remove() and discard().
ā¶ Show Solution
š” Solution:
s = {1, 2, 3, 4, 5}
s.add(6)
print(s) # {1, 2, 3, 4, 5, 6}
s.remove(3)
print(s) # {1, 2, 4, 5, 6}
s.discard(10) # No error if element not found
print(s) # {1, 2, 4, 5, 6}
š Explanation: add() inserts, remove() raises KeyError if missing, discard() doesn't.
Beginner Q6: Pop and Clear
Remove and return an arbitrary element using pop(), then clear the set.
ā¶ Show Solution
š” Solution:
s = {10, 20, 30, 40, 50}
removed = s.pop()
print(f"Removed: {removed}, Set: {s}")
s.clear()
print(f"After clear: {s}")
š Explanation: pop() removes arbitrary element, clear() empties the set.
Intermediate Q7: Union and Intersection
Find the union and intersection of two sets.
ā¶ Show Solution
š” Solution:
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
union = A.union(B)
intersection = A.intersection(B)
print(f"Union: {union}")
print(f"Intersection: {intersection}")
š Explanation: union() combines sets, intersection() finds common elements.
Intermediate Q8: Difference and Symmetric Difference
Find the difference and symmetric difference of two sets.
ā¶ Show Solution
š” Solution:
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
diff = A.difference(B)
sym_diff = A.symmetric_difference(B)
print(f"Difference (A - B): {diff}")
print(f"Symmetric Difference: {sym_diff}")
š Explanation: difference() removes elements in B, symmetric_difference() removes common elements.
Intermediate Q9: Subset and Superset
Check if one set is a subset or superset of another.
ā¶ Show Solution
š” Solution:
A = {1, 2, 3, 4, 5}
B = {1, 2, 3}
print(f"B is subset of A: {B.issubset(A)}") # True
print(f"A is superset of B: {A.issuperset(B)}") # True
š Explanation: issubset() checks if all elements are in other set, issuperset() checks opposite.
Advanced Q10: Update and Difference Update
Update a set with elements from another set, then remove elements using difference_update.
ā¶ Show Solution
š” Solution:
A = {1, 2, 3}
B = {3, 4, 5}
A.update(B)
print(f"After update: {A}") # {1, 2, 3, 4, 5}
A.difference_update({2, 4})
print(f"After difference_update: {A}") # {1, 3, 5}
š Explanation: update() adds elements, difference_update() removes elements in specified set.
3. Iteration Challenges 3 Questions
šÆ Focus: Iterate over sets effectively.
Beginner Q11: Simple Iteration
Iterate over a set and print each element.
ā¶ Show Solution
š” Solution:
colors = {"red", "green", "blue", "yellow"}
for color in colors:
print(color)
š Explanation: Sets are iterable, but order is not guaranteed.
Intermediate Q12: Iterate and Process
Iterate over a set of numbers and create a list of their squares.
ā¶ Show Solution
š” Solution:
nums = {1, 2, 3, 4, 5}
squares = []
for num in nums:
squares.append(num ** 2)
print(squares) # [1, 4, 9, 16, 25]
š Explanation: Iterates through set and builds a list.
Advanced Q13: Set of Sets Iteration
Iterate over a frozenset (hashable set) containing tuples.
ā¶ Show Solution
š” Solution:
data = {frozenset({1, 2}), frozenset({3, 4}), frozenset({5, 6})}
for fs in data:
for item in fs:
print(item, end=' ')
print()
š Explanation: Frozensets can be elements of sets, enabling nested iteration.
4. Set Comprehension Challenges 4 Questions
šÆ Focus: Use set comprehension for concise set creation.
Beginner Q14: Squares with Comprehension
Create a set of squares of numbers 1-10 using set comprehension.
ā¶ Show Solution
š” Solution:
squares = {x**2 for x in range(1, 11)}
print(squares) # {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
š Explanation: {expression for item in iterable}.
Beginner Q15: Filter with Comprehension
Create a set of numbers divisible by 3 from 1 to 30.
ā¶ Show Solution
š” Solution:
multiples = {x for x in range(1, 31) if x % 3 == 0}
print(multiples) # {3, 6, 9, 12, 15, 18, 21, 24, 27, 30}
š Explanation: With condition filter.
Intermediate Q16: Nested Comprehension
Create a set of all possible sums from two lists using nested comprehension.
ā¶ Show Solution
š” Solution:
A = [1, 2, 3]
B = [4, 5, 6]
sums = {a + b for a in A for b in B}
print(sums) # {5, 6, 7, 8, 9}
š Explanation: Nested comprehension with two for clauses.
Advanced Q17: Conditional with else
Create a set that categorizes numbers as "even" or "odd".
ā¶ Show Solution
š” Solution:
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = {f"{x} is even" if x % 2 == 0 else f"{x} is odd" for x in nums}
print(result)
š Explanation: Ternary operator inside comprehension.
5. Advanced Set Challenges 5 Questions
šÆ Focus: Combine multiple set techniques.
Advanced Q18: Find Common Elements
Find common elements between three sets.
ā¶ Show Solution
š” Solution:
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
C = {5, 6, 7, 8, 9}
common = A.intersection(B, C)
print(f"Common elements: {common}") # {5}
š Explanation: intersection() can take multiple arguments.
Advanced Q19: Remove Duplicates from List
Remove duplicates from a list while preserving the original order using set.
ā¶ Show Solution
š” Solution:
def unique_preserve_order(lst):
seen = set()
result = []
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
return result
data = [1, 2, 3, 2, 4, 1, 5, 3, 6]
print(unique_preserve_order(data)) # [1, 2, 3, 4, 5, 6]
š Explanation: Uses set for O(1) membership check while preserving order.
Advanced Q20: Set of Dictionaries
Create a set of dictionaries (using frozenset of items).
ā¶ Show Solution
š” Solution:
dict1 = {"name": "Alice", "age": 25}
dict2 = {"name": "Bob", "age": 30}
# Convert dictionaries to frozenset of items for hashing
set_of_dicts = {frozenset(dict1.items()), frozenset(dict2.items())}
for fs in set_of_dicts:
print(dict(fs))
š Explanation: Frozenset of items makes dictionaries hashable for sets.
Advanced Q21: Set Operations with Lists
Find elements present in first list but not in second, and vice versa.
ā¶ Show Solution
š” Solution:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
set1 = set(list1)
set2 = set(list2)
only_in_1 = set1 - set2
only_in_2 = set2 - set1
print(f"Only in list1: {only_in_1}")
print(f"Only in list2: {only_in_2}")
š Explanation: Set difference to find unique elements in each list.
Advanced Q22: Partition Set
Partition a set into two sets: one with even numbers and one with odd numbers.
ā¶ Show Solution
š” Solution:
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
evens = {x for x in nums if x % 2 == 0}
odds = nums - evens
print(f"Evens: {evens}")
print(f"Odds: {odds}")
š Explanation: Set comprehension and set difference for partitioning.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own set code.
========================================
SET PRACTICE
========================================
Unique numbers: {1, 2, 3, 4, 5, 6}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Squares: {1, 4, 9, 16, 25}
Colors:
- red
- green
- blue
ā
Write your set solutions here!
š Related Tutorials