Learn Python sets with this comprehensive tutorial. Discover how to create sets, perform set operations, remove duplicates, and solve real-world problems with practical examples and tasks. Perfect for beginners and students to master Python set concepts.
In Python, a set is an unordered collection of unique elements, meaning no duplicates are allowed. Sets are useful when you want to store multiple items but don’t need to keep them in a particular order, and you want to ensure that each item only appears once.
# Create an empty set
empty_set = set() # Note: {} creates an empty dictionary, not set!
# Create a set with elements
fruits = {'apple', 'banana', 'orange'}
numbers = {1, 2, 3, 4, 5}
# Convert list to set (removes duplicates)
colors = ['red', 'blue', 'green', 'blue', 'red']
unique_colors = set(colors) # {'red', 'blue', 'green'}
Video Tutorial: How to Create Empty Set in Python
# Creating a set
fruits = {"apple", "banana", "cherry", "apple"}
# Displaying the set
print(fruits) # Output: {'apple', 'banana', 'cherry'}
Notice how apple
only appears once, even though we tried to add it twice.
fruits = {'apple', 'banana'}
fruits.add('orange') # Adds single element
fruits.update(['kiwi', 'mango']) # Adds multiple elements
print(fruits) # {'apple', 'banana', 'orange', 'kiwi', 'mango'}
Real-world use: Adding new unique tags to a blog post.
numbers = {1, 2, 3, 4, 5}
numbers.remove(3) # Raises error if element doesn't exist
numbers.discard(10) # No error if element doesn't exist
popped = numbers.pop() # Removes and returns arbitrary element
numbers.clear() # Removes all elements
Video Tutorial: How to Add or Remove Elements in a Set
Real-world use: Removing items from a shopping cart while ensuring no duplicates.
Sets support mathematical operations like union, intersection, and difference.
|
): Combines elements from both sets.&
): Finds common elements between sets.-
): Finds elements in one set but not the other.a = {1, 2, 3}
b = {3, 4, 5}
# Union (elements in either set)
print(a | b) # {1, 2, 3, 4, 5}
# Intersection (elements in both sets)
print(a & b) # {3}
# Difference (elements in a but not b)
print(a - b) # {1, 2}
# Symmetric difference (elements in either set but not both)
print(a ^ b) # {1, 2, 4, 5}
Video Tutorial: Find the Union of Two Sets in Python
Real-world use: Finding common friends between two users (intersection).
vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels) # True
print('z' not in vowels) # True
Real-world use: Checking if a username is in a blocked list.
set1 = {1, 2}
set2 = {1, 2, 3}
print(set1 <= set2) # True (subset)
print(set2 >= set1) # True (superset)
print(set1.isdisjoint({4, 5})) # True (no common elements)
Real-world use: Checking if all required permissions are in a user’s permission set.
names = ['Alice', 'Bob', 'Alice', 'Charlie', 'Bob']
unique_names = list(set(names))
print(unique_names) # Order not guaranteed: ['Alice', 'Bob', 'Charlie']
user1_interests = {'music', 'movies', 'sports'}
user2_interests = {'books', 'music', 'travel'}
common_interests = user1_interests & user2_interests
print(f"You both like: {common_interests}") # {'music'}
valid_answers = {'yes', 'no', 'maybe'}
responses = ['yes', 'no', 'sometimes', 'maybe', 'no']
invalid = set(responses) - valid_answers
print(f"Invalid responses: {invalid}") # {'sometimes'}
Write a Python program that:
Example Output:
Numbers: {1, 2, 3, 4}
Write a Python program that:
Example Input:
Original List: [1, 2, 2, 3, 3, 4, 5]
Example Output:
Unique Numbers: {1, 2, 3, 4, 5}
Write a Python program that:
Example Input:
Original List: [1, 2, 2, 3, 4, 4, 5]
Expected Output:
Unique Numbers: {1, 2, 3, 4, 5}
Write a Python program that:
Expected Output:
Even Numbers: {2, 4, 6, 8, 10}
Prime Numbers: {2, 3, 5, 7}
Union: {2, 3, 4, 5, 6, 7, 8, 10}
Intersection: {2}
Tutorials, Roadmaps, Bootcamps & Visualization Projects