So far, we've explored lists and tuples for ordered sequences and dictionaries for key-value mappings. Now, let's look at Python's set
type, a collection designed specifically for storing unique items where the order doesn't matter.
Think of a set like a mathematical set: a collection of distinct objects. If you try to add an item that's already present in a set, nothing happens; the set remains unchanged because it only stores one copy of each unique item. This property makes sets particularly useful for tasks like removing duplicates from other collections or quickly checking if an item is part of a group.
Another defining characteristic of sets is that they are unordered. Unlike lists or tuples, sets do not maintain the elements in any specific sequence. This means you cannot access elements using an index (like my_list[0]
), as there's no defined "first" or "second" element.
You can create a set by enclosing comma-separated elements within curly braces {}
.
# Creating a set with unique elements
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers)
# Output might be {1, 2, 3, 4, 5} or some other order
# Creating a set with duplicates - duplicates are automatically removed
colors = {'red', 'green', 'blue', 'red'}
print(colors)
# Output: {'blue', 'red', 'green'} (order may vary)
Notice how 'red'
only appears once in the colors
set, even though we included it twice initially.
A small but important detail: creating an empty set requires using the set()
function without any arguments. Using empty curly braces {}
actually creates an empty dictionary, which we covered previously.
# Correct way to create an empty set
empty_s = set()
print(type(empty_s))
# Output: <class 'set'>
print(empty_s)
# Output: set()
# This creates an empty dictionary, NOT a set
empty_d = {}
print(type(empty_d))
# Output: <class 'dict'>
You can also create a set from any iterable object (like a list, tuple, or string) using the set()
function. This is a common technique for removing duplicate items from a sequence.
# Create a set from a list with duplicates
numbers_list = [1, 2, 2, 3, 4, 4, 4, 5]
unique_numbers_set = set(numbers_list)
print(unique_numbers_set)
# Output: {1, 2, 3, 4, 5} (order may vary)
# Create a set from a string
letters_set = set('hello')
print(letters_set)
# Output: {'h', 'l', 'o', 'e'} (order may vary)
Like lists, sets are mutable, meaning you can change their contents after they are created.
To add a single element to a set, use the .add()
method.
permissions = {'read', 'write'}
print(permissions)
# Output: {'write', 'read'} (order may vary)
# Add a new permission
permissions.add('execute')
print(permissions)
# Output: {'write', 'read', 'execute'} (order may vary)
# Try adding an existing element - no change
permissions.add('read')
print(permissions)
# Output: {'write', 'read', 'execute'} (order may vary)
There are two primary ways to remove elements:
.remove(element)
: This method removes the specified element. However, if the element is not found in the set, it raises a KeyError
..discard(element)
: This method also removes the specified element, but it does not raise an error if the element is not found. It simply does nothing in that case.Choosing between .remove()
and .discard()
depends on whether you need your program to stop if an attempt is made to remove a non-existent element (.remove()
), or if you prefer it to continue silently (.discard()
).
data_points = {10, 20, 30, 40}
# Remove an existing element using remove()
data_points.remove(20)
print(data_points)
# Output: {10, 40, 30} (order may vary)
# Try removing a non-existent element using remove() - This will cause an error
# data_points.remove(99) # Raises KeyError: 99
# Remove an existing element using discard()
data_points.discard(30)
print(data_points)
# Output: {10, 40} (order may vary)
# Try removing a non-existent element using discard() - No error
data_points.discard(99)
print(data_points)
# Output: {10, 40} (order may vary)
One of the most efficient operations with sets is checking if an element is present using the in
keyword. This is significantly faster than checking for membership in a list, especially for large collections.
allowed_users = {'alice', 'bob', 'charlie'}
# Check if a user is allowed
print('bob' in allowed_users)
# Output: True
print('eve' in allowed_users)
# Output: False
# Check if a user is NOT allowed
print('david' not in allowed_users)
# Output: True
Sets are the ideal choice when:
For example, if you have a list of user IDs from log entries and want a list of unique users who accessed a system:
log_entries = ['user1', 'user2', 'user1', 'user3', 'user2', 'user1']
# Use a set to find unique users
unique_users_set = set(log_entries)
print(unique_users_set)
# Output: {'user2', 'user3', 'user1'} (order may vary)
# If you need a list of unique users (order might not be original)
unique_users_list = list(unique_users_set)
print(unique_users_list)
# Output: ['user2', 'user3', 'user1'] (order may vary)
Sets provide a powerful and efficient way to handle collections where uniqueness and fast membership testing are the primary requirements. Next, we will look at the specific mathematical operations you can perform with sets.
© 2025 ApX Machine Learning