Sets
Think of a Set as a bag of unique items. Unlike a list, where things are lined up in a specific order, a set is just a collection where:
- Unordered: Items don’t have a specific “seat” or index.
- No Duplicates: If you try to add the same thing twice, the set will just ignore the second one.
- Unchangeable (mostly): You cannot change an item once it’s in the bag, but you can add new items or take old ones out.
Creating a Set
Sets are written with curly braces
{}.# Creating a set
fruits = {"apple", "banana", "cherry"}
# Sets ignore duplicates automatically
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # Output: {1, 2, 3}
Note: To create an empty set, you must use
set(), because {} is used to create an empty Dictionary.Access Set Items
Because sets are unordered, they do not have an index. You cannot ask for
my_set[0].- To see if an item is there, use the
inkeyword. - To see all items, you must loop through them.
colors = {"red", "green", "blue"}
# Check if 'red' is present
print("red" in colors) # Output: True
Adding and Removing Item
You can grow your “bag” of items using these methods:
.add(): To add one item..update(): To add multiple items (like another list or set).
.remove(): Removes an item. Warning: If the item isn’t there, it will crash your program..discard(): Removes an item. Safe: If the item isn’t there, it does nothing (no crash)..pop(): Removes a random item (since there is no order, you never know which one it will pick).
numbers = {1, 2, 3}
numbers.add(4) # {1, 2, 3, 4}
numbers.update([2, 5, 6]) # {1, 2, 3, 4, 5, 6}
numbers.remove(2) # {1, 3, 4, 5, 6}
numbers.discard(10) # No error even if 10 is not present
numbers.pop() # Removes something randomly
Set Operations
Sets are powerful because they let you do operations similar to math:
- Union (
|or.union()) → Combines items from both sets. - Intersection (
&or.intersection()) → Keeps only common items. - Difference (
-or.difference()) → Items in one set but not the other. - Symmetric Difference (
^) → Items that are in either set, but not both
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5} (union)
print(a & b) # {3} (intersection)
print(a - b) # {1, 2} (difference)
print(a ^ b) # {1, 2, 4, 5} (symmetric difference)
Frozenset
A Frozenset is exactly like a regular Set (it’s unordered and contains unique items), but with one major twist: it is immutable.
If a Set is like a dry-erase board where you can add and erase names, a Frozenset is like a stone monument—once the names are carved, they cannot be changed.
# Starting with a list
vowels_list = ['a', 'e', 'i', 'o', 'u']
# Freezing it
frozen_vowels = frozenset(vowels_list)
print(frozen_vowels) # Output: frozenset({'e', 'o', 'a', 'i', 'u'})
Why Use a Frozenset?
You might wonder, “Why lock a set?” There are two main reasons:
- Data Integrity: It ensures that a collection of data (like a list of restricted usernames or country codes) cannot be accidentally modified by another part of your program.
- Using Sets as Keys: In Python, you cannot use a regular Set as a key in a Dictionary because sets can change. However, because a Frozenset never changes, Python allows you to use it as a key.
Quick Summary Table
| Feature | set {} | frozenset() |
| Unique Items? | Yes | Yes |
| Unordered? | Yes | Yes |
| Changeable? | Yes (Mutable) | No (Immutable) |
| Can use as Dict Key? | No | Yes |
