- What is sets?
- Creating Sets
- Basic Operations
- Adding elements to a set
- B. Removing elements from a set
- C. Checking membership with in keyword
- IV. Set Operations
- V. Set Methods
- VI. Iterating Through a Set
- VII. Common Use Cases
- VIII. Practical Examples
- IX. Summary
- X. Exercises
- Exercise 1: Set Operations
- Exercise 2: Removing Duplicates
- Exercise 3: Checking Subsets
- XI. Further Reading
What is sets?
You can think of “sets” as simply a group. “Sets” makes sure you don’t repeat the same data. It only keeps unique pieces of information in a group, so there’s only one of each. Additionally, sets don’t follow a specific order, so you can’t rely on the arrangement of elements.
In Python, we use sets for a couple of important reasons:
- Ensuring Uniqueness: As mentioned early, sets only allow unique elements. If you have a list of items and you want to make sure there are no duplicates, using a set will automatically remove them.
- Efficient Membership Testing: Sets are fast when checking whether a specific element is present in the set. This is useful when you need to quickly verify if something is part of a group without having to go through each element one by one.
Sets help us maintain uniqueness in a collection and provide an efficient way to check for the presence of elements.
Creating Sets
Syntax for creating sets
Sets are created using curly braces `{}` or the `set()`
Creating an empty set
c = {}
c is an empty set, but it is actually a dictionary that you will learn later in this article.
type(c)
Creating a set with elements
a = {2, 5, 1, 3, 4}
b = {"apple", "banana", "orange", "peach"}
Also, if you try to register multiple sets of the same data, nothing will happen. No error will occur.
b = {"apple", "banana", "banana", "orange", "banana", "peach"}
print(b)
Output:
{'banana', 'apple', 'peach', 'orange'}
Creating a set from a list
Converting a list into a set is a common operation in Python, especially when you want to work with unique elements or eliminate duplicate values. The process is straightforward and can be accomplished using the `set()` constructor.
sample_list = [1, 2, 3, 3, 4, 5, 5, 6]
sample_set = set(sample_list)
print("List: ", sample_list)
print("Set from list: ", sample_set)
Output:
List: [1, 2, 3, 3, 4, 5, 5, 6]
Set from list: {1, 2, 3, 4, 5, 6}
Practical Example: Removing Duplicates
One common application of creating sets from lists is removing duplicate values. Let’s consider a real-world scenario where you have a list of user IDs, and you want to eliminate any redundant entries.
# Original list with duplicate user IDs
user_ids = [101, 102, 103, 102, 104, 105, 101, 106]
# Create a set to remove duplicates
unique_user_ids = set(user_ids)
# Display the results
print("Original User IDs:", user_ids)
print("Unique User IDs:", unique_user_ids)
Output:
Original User IDs: [101, 102, 103, 102, 104, 105, 101, 106]
Unique User IDs: {101, 102, 103, 104, 105, 106}
By converting the list into a set, you effortlessly obtain a collection of unique user IDs, providing a cleaner and more efficient representation of your data.
Basic Operations
Adding elements to a set
1. Using add() method
2. Using update() method
B. Removing elements from a set
1. Using remove() method
2. Using discard() method
C. Checking membership with in keyword
IV. Set Operations
A. Union
1. Using union()
method
2. Using |
operator
B. Intersection
1. Using intersection()
method
2. Using &
operator
C. Difference
1. Using difference()
method
2. Using -
operator
D. Symmetric Difference
1. Using symmetric_difference()
method
2. Using ^
operator
V. Set Methods
A. clear()
: Remove all elements from a set
B. copy()
: Create a shallow copy of a set
C. pop()
: Remove and return an arbitrary element
D. len()
: Get the number of elements in a set
VI. Iterating Through a Set
A. Using a for
loop
B. Example: Iterating through a set and printing elements
VII. Common Use Cases
A. Removing duplicates from a list using a set
B. Checking for unique elements in a collection
VIII. Practical Examples
A. Example: Finding common elements in two sets
B. Example: Checking if a set is a subset of another set
IX. Summary
A. Recap of key concepts
B. Emphasize the usefulness of sets in specific scenarios
X. Exercises
A. Practice problems for the readers to reinforce their understanding
Certainly! Here are three practical exercises for readers to review the chapter on sets in Python:
Exercise 1: Set Operations
Problem: Given two sets set_a = {1, 2, 3, 4, 5}
and set_b = {3, 4, 5, 6, 7}
, perform the following operations and print the results:
- Find the union of the two sets.
- Find the intersection of the two sets.
- Find the elements that are present in
set_a
but not inset_b
. - Find the symmetric difference between the two sets.
# Exercise 1: Set Operations
set_a = {1, 2, 3, 4, 5}
set_b = {3, 4, 5, 6, 7}
# 1. Find the union of the two sets.
union_result = set_a.union(set_b)
print("Union:", union_result)
# 2. Find the intersection of the two sets.
intersection_result = set_a.intersection(set_b)
print("Intersection:", intersection_result)
# 3. Find the elements that are present in set_a but not in set_b.
difference_result = set_a - set_b
print("Set_a - Set_b:", difference_result)
# 4. Find the symmetric difference between the two sets.
symmetric_difference_result = set_a.symmetric_difference(set_b)
print("Symmetric Difference:", symmetric_difference_result)
Exercise 2: Removing Duplicates
Problem: Create a list numbers = [1, 2, 3, 4, 3, 2, 5, 6, 7, 8, 7]
. Use a set to remove duplicates from the list and print the updated list without duplicates.
# Exercise 2: Removing Duplicates
numbers = [1, 2, 3, 4, 3, 2, 5, 6, 7, 8, 7]
# Use a set to remove duplicates
unique_numbers = list(set(numbers))
print("Original List:", numbers)
print("List without Duplicates:", unique_numbers)
Exercise 3: Checking Subsets
Problem: Create three sets: set_x = {1, 2, 3, 4, 5}
, set_y = {2, 3}
, and set_z = {6, 7, 8}
. Write Python code to check the following:
- Check if
set_y
is a subset ofset_x
. - Check if
set_z
is a subset ofset_x
. - Check if
set_x
is a superset ofset_y
. - Check if
set_x
is a superset ofset_z
.
# Exercise 3: Checking Subsets
set_x = {1, 2, 3, 4, 5}
set_y = {2, 3}
set_z = {6, 7, 8}
# 1. Check if set_y is a subset of set_x.
is_subset_y = set_y.issubset(set_x)
print("Is set_y a subset of set_x?", is_subset_y)
# 2. Check if set_z is a subset of set_x.
is_subset_z = set_z.issubset(set_x)
print("Is set_z a subset of set_x?", is_subset_z)
# 3. Check if set_x is a superset of set_y.
is_superset_y = set_x.issuperset(set_y)
print("Is set_x a superset of set_y?", is_superset_y)
# 4. Check if set_x is a superset of set_z.
is_superset_z = set_x.issuperset(set_z)
print("Is set_x a superset of set_z?", is_superset_z)
These exercises will allow readers to apply the concepts learned in the chapter and reinforce their understanding of set operations, removing duplicates, and checking subsets.
XI. Further Reading
A. Recommend additional resources for deeper exploration
Remember to include plenty of example code and encourage readers to experiment with sets in their own Python environments. Exercises can be based on real-world scenarios to help readers apply their knowledge.