A set in Python is a collection data type.
Key Features of Sets
- Unordered: The items in a set have no defined order.
- Unindexed: You cannot access items in a set by index.
- Unique: No duplicate elements are allowed.
- Mutable: You can add and remove items.
How to Define a Set
Sets are implemented using a hash table, making membership testing and operations like union and intersection very fast.
- Creating a Set Using Curly Braces {}:
my_set = {1, 2, 3, 4} print(my_set) # Output: {1, 2, 3, 4}
- Using set() Constructor:
my_set = set([1, 2, 2, 3]) # Duplicates are removed print(my_set) # Output: {1, 2, 3} empty_set = set() # Creates an empty set
Basic Operations on Sets
- Adding Elements
- add(element): Adds a single element to the set.
my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
- Removing Elements
- remove(element): Removes the specified element. Raises KeyError if the element is not found.
my_set.remove(2) discard(element): Removes the specified element without raising an error if it does not exist. my_set.discard(5) # No error even if 5 is not in the set
- pop(): Removes and returns an arbitrary element from the set.
removed_item = my_set.pop() print(removed_item)
- clear(): Removes all elements from the set.
my_set.clear()
Set Advance Operations
- Union Combines all unique elements from both sets:
set1 = {1, 2, 3} set2 = {3, 4, 5} print(set1 | set2) # Output: {1, 2, 3, 4, 5} print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
- Intersection Returns elements common to both sets:
print(set1 & set2) # Output: {3} print(set1.intersection(set2)) # Output: {3}
- Difference Returns elements in the first set but not in the second:
print(set1 - set2) # Output: {1, 2} print(set1.difference(set2)) # Output: {1, 2}
- Symmetric Difference Returns elements in either set but not in both:
print(set1 ^ set2) # Output: {1, 2, 4, 5} print(set1.symmetric_difference(set2)) # Output: {1, 2, 4, 5}
Membership Testing
my_set = {1, 2, 3}
print(1 in my_set) # Output: True
print(5 not in my_set) # Output: True
Iterating Through a Set
my_set = {1, 2, 3}
for item in my_set:
print(item)
Frozenset
A frozenset is an immutable version of a set. Once created, its elements cannot be modified.
fs = frozenset([1, 2, 3])
print(fs) # Output: frozenset({1, 2, 3})
# fs.add(4) # Raises an AttributeError
Set Methods
Method | Description |
add(element) | Adds an element to the set. |
remove(element) | Removes an element; raises KeyError if not found. |
discard(element) | Removes an element; does nothing if not found. |
pop() | Removes and returns a random element. |
clear() | Removes all elements from the set. |
union(set) | Returns the union of two sets. |
intersection(set) | Returns the intersection of two sets. |
difference(set) | Returns the difference between two sets. |
symmetric_difference(set) | Returns the symmetric difference of two sets. |
Examples of Usage
- Removing Duplicates from a List
numbers = [1, 2, 2, 3, 4, 4] unique_numbers = set(numbers) print(unique_numbers) # Output: {1, 2, 3, 4}
- Finding Common Items Between Two Lists
list1 = [1, 2, 3] list2 = [3, 4, 5] common_items = set(list1) & set(list2) print(common_items) # Output: {3}
When to Use Sets
- Removing duplicates from a collection.
- Performing fast membership testing.
- Working with mathematical set operations.
- Use cases where element ordering or duplicates are not required.
Sets are highly efficient for tasks requiring uniqueness and membership tests!