Sets in Python
You’ve now seen three ways to store multiple values: lists, tuples, and dictionaries. There’s one more built-in type worth knowing: the set. A set is similar to a list, but with two big differences: it never allows duplicate values, and it doesn’t keep items in any particular order.
What a set actually is
You create a set with curly braces, similar to a dictionary, but without key-value pairs, just values.
fruits = {"apple", "banana", "mango"}
print(fruits)
""" output
{'banana', 'mango', 'apple'}"""Notice the order isn’t guaranteed to match how you typed it. Sets don’t track position the way lists do, so you can’t rely on items appearing in any particular sequence.
Duplicates are automatically removed
This is the main reason people reach for a set.
numbers = {1, 2, 2, 3, 3, 3, 4}
print(numbers)
""" output
{1, 2, 3, 4}"""Even though 2 and 3 were typed multiple times, the set only keeps one copy of each. This makes sets genuinely useful for removing duplicates from existing data.
scores = [85, 90, 85, 70, 90, 100]
unique_scores = set(scores)
print(unique_scores) # {100, 70, 85, 90}Adding and removing items
scores = [85, 90, 85, 70, 90, 100]
unique_scores = set(scores)
print(unique_scores) # {100, 70, 85, 90}remove() throws an error if the value doesn’t exist. If you’d rather avoid that, use discard() instead, which does nothing if the value isn’t found.
fruits.discard("grape") # no error, even though grape isn't in the setChecking membership
fruits = {"apple", "banana", "mango"}
print("banana" in fruits) # True
print("grape" in fruits) # FalseSame in operator as always, and it’s actually faster on a set than on a list once your data gets large, since Python checks set membership differently under the hood.
Set operations: union, intersection, difference
This is where sets genuinely earn their place, they let you compare groups of data the way you would in basic math.
class_a = {"Sujan", "Priya", "Anish"}
class_b = {"Priya", "Kabin", "Anish"}
print(class_a | class_b) # union: everyone in either class
print(class_a & class_b) # intersection: students in both classes
print(class_a - class_b) # difference: in class_a but not class_b"""Outputs
{'Sujan', 'Priya', 'Anish', 'Kabin'}
{'Priya', 'Anish'}
{'Sujan'}"""| combines both sets without duplicates. & Gives you only what’s shared between them. - Gives you what’s in the first set but missing from the second.
Looping through a set
fruits = {"apple", "banana", "mango"}
for fruit in fruits:
print(fruit)Works the same as looping through a list; just remember the order printed isn’t guaranteed to match any particular sequence.
Python Sets Practice Questions
1. Create a set of your favorite colors, then add one more color to it.
2. Write code that removes duplicate values from this list using a set: [1, 3, 3, 5, 5, 5, 7]
3. What is the output of the following code?
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)
print(a | b)
print(a - b)4. What’s the difference between using remove() and discard() on a value that doesn’t exist in a set?
5. Write a program that checks whether “Nepal” exists in a set of country names.
6. Explain, in your own words, one real situation where you’d choose a set over a list.
