Data Structures: Sets
Discover sets, a data structure for storing unordered collections of unique elements. Covers set operations like union, intersection, and difference.
Data Structures: Sets in Python
Discover sets, a data structure for storing unordered collections of unique elements. Covers set operations like union, intersection, and difference.
What is a Set?
In Python, a set is an unordered collection of unique elements. This means that a set cannot contain duplicate values. Sets are useful for tasks that involve membership testing, eliminating duplicates, and performing mathematical set operations.
Key Characteristics of Sets:
- Unordered: The elements in a set have no specific order.
- Unique: Sets only contain unique elements; duplicates are automatically removed.
- Mutable: You can add or remove elements from a set after it's created.
- Hashable Elements: Set elements must be immutable (hashable) objects like numbers, strings, and tuples. Lists and dictionaries cannot be elements of a set.
Creating Sets
You can create a set in Python using the following methods:
- Using curly braces
{}
: - Using the
set()
constructor: - Creating an empty set: You must use
set()
to create an empty set. Using{}
creates an empty dictionary.
my_set = {1, 2, 3}
print(my_set) # Output: {1, 2, 3}
my_set = set([1, 2, 3]) # From a list
print(my_set) # Output: {1, 2, 3}
my_set = set((1, 2, 3)) # From a tuple
print(my_set) # Output: {1, 2, 3}
my_set = set("hello") # From a string
print(my_set) # Output: {'o', 'l', 'e', 'h'}
empty_set = set()
print(empty_set) # Output: set()
print(type(empty_set)) # Output: <class 'set'>
# Incorrect way to create an empty set (creates a dictionary)
empty_dictionary = {}
print(type(empty_dictionary)) # Output: <class 'dict'>
Basic Set Operations
Python provides several built-in methods for performing set operations:
1. Adding Elements
You can add elements to a set using the add()
method:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
my_set.add(3) #Adding a duplicate has no effect
print(my_set) # Output: {1, 2, 3, 4}
2. Removing Elements
You can remove elements from a set using the remove()
or discard()
methods:
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4}
# Trying to remove an element that doesn't exist raises a KeyError
# my_set.remove(5) # Raises KeyError
my_set.discard(4)
print(my_set) # Output: {1, 2}
# discard() does not raise an error if the element doesn't exist
my_set.discard(5)
print(my_set) #Output: {1, 2} (no change, no error)
3. Union
The union of two sets contains all elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
#Using the | operator
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5}
4. Intersection
The intersection of two sets contains only the elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
#Using the & operator
intersection_set = set1 & set2
print(intersection_set) # Output: {3}
5. Difference
The difference between two sets contains the elements that are in the first set but not in the second set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}
#Using the - operator
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}
6. Symmetric Difference
The symmetric difference of two sets contains elements which are in either of the sets, but not in their intersection.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
#Using the ^ operator
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
7. Subset and Superset
You can check if one set is a subset or superset of another.
set1 = {1, 2}
set2 = {1, 2, 3, 4}
print(set1.issubset(set2)) # Output: True
print(set2.issuperset(set1)) # Output: True
#Using <= for subset and >= for superset
print(set1 <= set2) # Output: True
print(set2 >= set1) # Output: True
8. Membership Testing
You can use the in
operator to check if an element is present in a set.
my_set = {1, 2, 3}
print(1 in my_set) # Output: True
print(4 in my_set) # Output: False
When to Use Sets
- Removing Duplicates: If you need to ensure that a collection only contains unique elements, sets are a natural choice.
- Membership Testing: Sets offer very efficient (O(1) on average) membership testing, making them ideal for checking if an item is present in a collection.
- Mathematical Set Operations: Sets make it straightforward to perform union, intersection, difference, and symmetric difference operations.
Conclusion
Sets are a powerful and efficient data structure in Python for managing collections of unique elements and performing set operations. Understanding how to use sets effectively can simplify your code and improve its performance.