4.1 Getting Subsets by Rule
A subset is a set where all elements belong to another, larger set. Python's got some built-in methods and operators for dealing with subsets. I'll show you how to get subsets, check if one set is a subset of another, and use this knowledge in different scenarios.
Using a for
Loop
You can create an empty set and add elements that satisfy a condition using a for
loop.
my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_set = set()
for x in my_set:
if x % 2 == 0:
even_set.add(x)
print(even_set) # Output: {2, 4, 6, 8, 10}
This is the simplest and most obvious way to create a subset. But there are a few other, more compact ways.
Using the filter()
Function
The filter()
function applies a function to each element and returns only those elements for which the function returned True. You'll need to convert the result back into a set.
my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_set = set(filter(lambda x: x % 2 == 0, my_set))
print(even_set) # Output: {2, 4, 6, 8, 10}
Inside the filter()
function, we're using a lambda expression—a short way to define an inline function or filtering rule. I'll dive more into lambdas in a few lectures.
4.2 Using Set Comprehensions
Remember List Comprehension
? Where we used square brackets to quickly generate a list and its elements? This syntax looked like:
[expression for variable in sequence]
Where:
-
variable
— the identifier of some variable, -
sequence
— a sequence of values that the variable will take (this could be a list, string, or an object obtained via therange
function), -
expression
— an expression, usually dependent on the variable used in the generator, to populate the list elements.
For sets, there's a similar function, but use curly braces:
{expression for variable in sequence}
Set comprehensions allow you to easily and concisely create new sets based on existing ones, applying filtering conditions.
Here's how to select only even elements from a set:
my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_set = {x for x in my_set if x % 2 == 0}
print(even_set) # Output: {2, 4, 6, 8, 10}
And here's how to select only strings:
my_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, "apple", "banana"}
even_set = {x for x in my_set if type(x) == str}
print(even_set) # Output: {"apple", "banana"}
4.3 Checking for an Element
One of the basic operations you can perform with sets is checking for an element in a set. Python offers several ways to do this. Let's look at different methods to check if an element is in a set.
Using the in
Operator
The most common and convenient way to check for an element in a set is using the in
operator. This method returns True
if the element is in the set, and False
otherwise.
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # Output: True
print(6 in my_set) # Output: False
Using the not in
Operator
The not in
operator is the opposite of in
and is used to check that an element is not in the set. It returns True
if the element is not present, and False
otherwise.
my_set = {1, 2, 3, 4, 5}
print(6 not in my_set) # Output: True
print(3 not in my_set) # Output: False
Using Loops
Although using loops to check for an element in a set isn't the most efficient way, it can be useful in situations where you're working with more complex data structures or performing additional operations.
my_set = {1, 2, 3, 4, 5}
element = 3
found = False
for item in my_set:
if item == element:
found = True
break
print(found) # Output: True
4.4 Checking Set Nesting
Python provides the <=
operator and the issubset()
method for checking if one set is a subset of another.
Using the <=
Operator
The <=
operator lets you easily check if one set is a subset of another.
set_a = {1, 2, 3}
set_b = {1, 2, 3, 4, 5}
print(set_a <= set_b) # Output: True
print(set_b <= set_a) # Output: False
Using the issubset()
Method
The issubset()
method does the same thing as the <=
operator, returning True
if all elements of one set are contained in another.
set_a = {1, 2, 3}
set_b = {1, 2, 3, 4, 5}
print(set_a.issubset(set_b)) # Output: True
print(set_b.issubset(set_a)) # Output: False
Checking for Supersets
Similarly to subsets, you can check if one set is a superset of another using the >=
operator and the issuperset()
method.
Using the >=
Operator
set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}
print(set_a >= set_b) # Output: True
print(set_b >= set_a) # Output: False
Using the issuperset()
Method
set_a = {1, 2, 3, 4, 5}
set_b = {1, 2, 3}
print(set_a.issuperset(set_b)) # Output: True
print(set_b.issuperset(set_a)) # Output: False
GO TO FULL VERSION