In this lesson, we will explore Sets, which are another important built-in data structure in Python. A set is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate values, and they are useful when you need to store items that are unique and don’t need to maintain any specific order.
By the end of this lesson, you will understand how to create sets, perform set operations (such as union, intersection), and use various set methods to manipulate the elements.
1. Creating Sets
A set is a collection of elements that is unordered and does not allow duplicates. You can create a set using curly braces {} or the set() constructor.
Syntax:
# or
set_name = set([element1, element2, …])
Example:
# Creating a set using curly braces
fruits = {“apple”, “banana”, “cherry”}
print(fruits) # Output: {‘apple’, ‘banana’, ‘cherry’}
# Creating a set using the set() constructor
numbers = set([1, 2, 3, 4, 5])
print(numbers) # Output: {1, 2, 3, 4, 5}
# Creating an empty set
empty_set = set()
print(empty_set) # Output: set()
- Important Note:
- Sets do not allow duplicate values. If you try to add duplicate items, Python will automatically remove the duplicates.
- Sets are unordered, meaning there is no specific order to the elements stored in a set.
- You cannot access elements by index in a set like you can with lists, because sets are unordered.
Examples of Duplicates Being Removed:
print(fruits) # Output: {‘apple’, ‘banana’} (duplicate ‘apple’ is removed)
numbers = set([1, 2, 2, 3, 3, 3, 4])
print(numbers) # Output: {1, 2, 3, 4}
2. Set Operations
Sets come with several built-in operations that allow you to perform common mathematical set operations. These operations help you work with multiple sets at the same time, making it easier to perform tasks such as combining sets, finding common elements, and identifying differences.
Union of Sets (union(), |):
The union of two sets is a set containing all the elements from both sets, excluding duplicates.
- Using union()method:
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
- Using the |operator:
print(union_set) # Output: {1, 2, 3, 4, 5}
Intersection of Sets (intersection(), &):
The intersection of two sets is a set containing only the elements that are common to both sets.
- Using intersection()method:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
- Using the &operator:
print(intersection_set) # Output: {3}
Difference of Sets (difference(), –):
The difference of two sets is a set containing elements that are in the first set but not in the second.
- Using difference()method:
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}
Symmetric Difference of Sets (symmetric_difference(), ^):
The symmetric difference of two sets is a set containing elements that are in either of the sets, but not in both.
- Using symmetric_difference()method:
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
- Using the ^operator:
print(symmetric_difference_set) # Output: {1, 2, 4, 5}
Subset and Superset (issubset(), issuperset()):
- A set Ais a subset of another set B if all elements of A are also in B.
- A set Ais a superset of another set B if all elements of B are also in A.
set2 = {1, 2, 3, 4, 5}
# Checking if set1 is a subset of set2
print(set1.issubset(set2)) # Output: True
# Checking if set2 is a superset of set1
print(set2.issuperset(set1)) # Output: True
3. Set Methods
Sets come with several built-in methods that you can use to manipulate the set. Let’s look at the most commonly used methods.
add():
The add() method is used to add an element to a set.
fruits.add(“cherry”)
print(fruits) # Output: {‘apple’, ‘banana’, ‘cherry’}
remove():
The remove() method is used to remove an element from the set. If the element is not present in the set, it will raise a KeyError.
fruits.remove(“banana”)
print(fruits) # Output: {‘apple’, ‘cherry’}
discard():
The discard() method is similar to remove(), but it does not raise an error if the element is not found in the set.
fruits.discard(“pear”) # No error raised, even though ‘pear’ is not in the set
print(fruits) # Output: {‘apple’, ‘banana’, ‘cherry’}
pop():
The pop() method removes and returns an arbitrary element from the set. Since sets are unordered, you cannot predict which element will be removed.
removed_item = fruits.pop()
print(fruits) # Output: {‘banana’, ‘cherry’} (The removed item is unpredictable)
print(removed_item) # Output: ‘apple’ (or another item)
clear():
The clear() method removes all elements from the set, making it an empty set.
fruits = {“apple”, “banana”, “cherry”}
fruits.clear()
print(fruits) # Output: set()
copy():
The copy() method returns a shallow copy of the set.
new_fruits = fruits.copy()
print(new_fruits) # Output: {‘apple’, ‘banana’, ‘cherry’}
4. When to Use Sets
Sets are useful when:
- You need to store unique elements and avoid duplicates.
- You need to perform mathematical set operations like union, intersection, or difference.
- You are working with data where the order does not matter, and only membership is important.
Some real-world scenarios where sets are used:
- Storing unique tags in a content management system (e.g., a blog system where each post has a set of unique tags).
- Analyzing social media data to find mutual followers (intersection of sets).
- Performing analysis on datasets that require finding unique elements or performing comparisons between multiple datasets.
5. Conclusion
In this lesson, you have learned:
- How to createsets and perform basic set operations like union, intersection, and difference.
- How to use common set methodsto manipulate the set, such as adding, removing, and copying elements.
- The importance of sets in Python and their real-world applications.
Sets are highly efficient when it comes to operations involving membership testing and eliminating duplicates. In the next lesson, we will explore more advanced topics in Python’s data structures.
Leave a Reply