Set in Python
A Set in Python is a built-in data structure used to store multiple unique elements in a single variable. Sets are unordered and mutable, making them an efficient tool for membership tests, eliminating duplicates, and performing mathematical set operations.
Why Use Sets?β
Sets are commonly used when:
- You need to store unique values only
- The order of elements does not matter
- You want to perform fast membership testing
- You need efficient union, intersection, and difference operations
Key Characteristicsβ
- Unordered: Elements have no defined order.
- Unique: No duplicates allowed.
- Mutable: You can add or remove items.
- Iterable: You can loop through a set.
Creating a Setβ
There are two main ways to create a set:
Using Curly Braces {}β
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'banana', 'apple', 'cherry'}
Using the set() Constructorβ
numbers = set([1, 2, 3, 2])
print(numbers) # Output: {1, 2, 3}
Important: Empty curly braces
{}create an empty dictionary, not a set. Useset()instead:
empty_set = set()
Adding Elements to a Setβ
You can add elements using the add() method:
colors = {"red", "green"}
colors.add("blue")
print(colors) # Output: {'red', 'green', 'blue'}
Removing Elements from a Setβ
Python provides several methods to remove elements:
remove()β
Removes the specified item. Raises an error if the item does not exist.
colors = {"red", "green"}
colors.remove("red")
print(colors) # Output: {'green'}
If you try to remove an item not present:
colors.remove("yellow") # KeyError
discard()β
Removes the specified item without raising an error if the item is not found.
colors = {"green"}
colors.discard("yellow") # No error
pop()β
Removes and returns an arbitrary element.
colors = {"red", "blue"}
removed = colors.pop()
print("Removed:", removed)
print("Remaining:", colors)
clear()β
Removes all elements.
colors.clear()
print(colors) # Output: set()
Updating a Setβ
To add multiple items at once, use update():
a = {1, 2}
a.update([3, 4], {5, 6})
print(a) # Output: {1, 2, 3, 4, 5, 6}
Membership Testingβ
Check if an item is in a set using in or not in:
nums = {1, 2, 3}
print(2 in nums) # Output: True
print(5 not in nums) # Output: True
Common Set Operationsβ
Python sets support powerful operations:
| Operation | Syntax | Description | |
|---|---|---|---|
| Union | `a | bora.union(b)` | Combine all elements from both sets |
| Intersection | a & b or a.intersection(b) | Elements common to both sets | |
| Difference | a - b or a.difference(b) | Elements in a but not in b | |
| Symmetric Difference | a ^ b or a.symmetric_difference(b) | Elements in either set, but not both |
Exampleβ
a = {1, 2, 3}
b = {3, 4, 5}
# Union
print(a | b) # {1, 2, 3, 4, 5}
# Intersection
print(a & b) # {3}
# Difference
print(a - b) # {1, 2}
# Symmetric Difference
print(a ^ b) # {1, 2, 4, 5}
Iterating Over a Setβ
Loop through elements with a for loop:
animals = {"cat", "dog", "rabbit"}
for animal in animals:
print(animal)
Since sets are unordered, the output order may differ every time.
Frozensetβ
A frozenset is an immutable version of a set. Once created, you cannot modify it.
fs = frozenset([1, 2, 3])
print(fs) # frozenset({1, 2, 3})
# fs.add(4) # AttributeError
Use frozenset when you need a hashable set, e.g., as dictionary keys.
When to Use Sets?β
- Removing duplicates from lists
- Fast membership tests
- Performing set algebra (union, intersection)
- Representing unique collections
Best Practicesβ
- Use
set()to create empty sets. - Use
discard()if you are not sure whether an element exists. - Prefer sets over lists when you need uniqueness or fast lookups.
Practice Exercisesβ
- Create a set of your favorite fruits. Add one more fruit and remove another.
- Find the union and intersection of
{1, 2, 3}and{3, 4, 5}. - Write a program that removes duplicates from a list using a set.
- Create a frozenset and show that it cannot be modified.
Summary Tableβ
| Feature | Description |
|---|---|
| Mutable | Yes (can add/remove elements) |
| Ordered | No |
| Duplicates | Not allowed |
| Empty Declaration | set() |
| Immutable Variant | frozenset |
| Typical Use Cases | Membership tests, uniqueness, set operations |