Legends ofPythos
Claim your name
Working with Data

Sets and uniqueness

Lesson 4 of 14

Watch the lesson1:28 ยท with Torsten
Mental model: Think of a set as a bag of marbles where no two marbles are identical. If you drop in another red marble, the bag still holds just one. You cannot ask for "the second item" because there is no order; you can only check if a specific color exists.
Why this matters: Real programs constantly need to track unique entities without duplicates: the set of logged-in user IDs, distinct error codes in a log file, or available seats on a flight. Sets handle these scenarios faster and cleaner than lists.

Creating Unique Collections

# Using curly braces
scores = {'A', 'B', 'C'}
print(scores)

# Converting a list with duplicates
raw_temps = [20, 21, 21, 22]
distinct_temps = set(raw_temps)
print(distinct_temps)
Sets automatically remove duplicate values
The first line creates a set directly. The second converts a list, stripping out the repeated 21. Notice that you cannot index into a set like scores[0] because items have no fixed position.

Adding and Removing Items

# Add new unique items
allowed_users = {'alice', 'bob'}
allowed_users.add('charlie')
print(allowed_users)

# Safe removal: discard does nothing if missing
allowed_users.discard('dave')  # no error raised
print(allowed_users)
Managing set contents safely
add() inserts a new item or ignores it if already present. discard() removes an item if it exists but stays silent otherwise, making it ideal for cleanup tasks where you are not sure what is currently in the set.

Set Operations

# Union: all unique items from both
python_devs = {'alice', 'bob'}
java_devs = {'bob', 'charlie'}
all_developers = python_devs | java_devs
print(all_developers)

# Intersection: common to both
polyglots = python_devs & java_devs
print(polyglots)
Combining and finding overlaps
| merges two sets into one unique collection. & finds the overlap, useful for identifying who can speak both languages.
python_devs = {'alice', 'bob'}
java_devs = {'bob', 'charlie'}

# Difference: items in first but not second
python_only = python_devs - java_devs
print(python_only)
Finding exclusive members
- subtracts the second set from the first. This is perfect for finding who knows Python but not Java.

Your turn

0 of 3 solved

Exercise 1

+25 XP
Create a set called 'letters' containing the unique characters from the string 'hello world'. Then add the character 'z' to it. Print the resulting set.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+25 XP
Given the list [1, 2, 2, 3, 4, 4, 4], remove the duplicates by converting it to a set called unique_nums. Then find the intersection of that set with {3, 5, 7}, store it in a variable called common, and print how many items common has.
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+25 XP
Create two sets: 'taken' = {'a', 'b'} and 'available' = {'c', 'd', 'e'}. Find the difference (items in available but not taken). Then add all items from this result to a new set called 'free'. Print whether 'a' is in free.
Write your Python here.

Run your code to check it against the tests.