In Python, sets are powerful collections that allow you to perform a variety of mathematical operations such as union, intersection, and difference. These operations are essential when working with sets, and Python provides built-in methods to perform these operations. This article explores some common set methods, including union()
, intersection()
, and difference()
, along with examples.
union()
MethodThe union()
method returns a new set that contains all the unique elements from two or more sets. This method effectively combines the sets by including every element from each set, removing duplicates automatically.
union()
# Union of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.union(set2) print(result) # Outputs: {1, 2, 3, 4, 5}
In this example, the union of set1
and set2
includes all the elements from both sets, with duplicates removed.
intersection()
MethodThe intersection()
method returns a new set containing only the elements that are present in both sets. If there are no common elements, the result will be an empty set.
intersection()
# Intersection of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.intersection(set2) print(result) # Outputs: {3}
In this example, the intersection of set1
and set2
returns the element 3
as it is the only common element between the two sets.
difference()
MethodThe difference()
method returns a new set that contains the elements that are in the first set but not in the second set. If there are no unique elements, the result will be an empty set.
difference()
# Difference of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.difference(set2) print(result) # Outputs: {1, 2}
In this example, the difference of set1
and set2
returns the elements 1
and 2
that are in set1
but not in set2
.
symmetric_difference()
MethodThe symmetric_difference()
method returns a new set that contains the elements that are in either of the sets, but not in both. It is essentially the union of the differences of both sets.
symmetric_difference()
# Symmetric difference of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.symmetric_difference(set2) print(result) # Outputs: {1, 2, 4, 5}
In this example, the symmetric difference of set1
and set2
returns the elements that are only in one of the sets, excluding the common elements 3
.
issubset()
and issuperset()
MethodsThese methods are used to compare sets and check if one set is a subset or superset of another.
issubset()
and issuperset()
# Checking if a set is a subset or superset set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} set3 = {4, 5} print(set1.issubset(set2)) # Outputs: True (set1 is a subset of set2) print(set2.issuperset(set1)) # Outputs: True (set2 is a superset of set1) print(set2.issuperset(set3)) # Outputs: True (set2 is a superset of set3)
In this example, set1
is a subset of set2
, and set2
is a superset of both set1
and set3
.
isdisjoint()
MethodThe isdisjoint()
method checks if two sets have no elements in common. If the sets do not share any elements, it returns True
, otherwise it returns False
.
isdisjoint()
# Checking if two sets are disjoint set1 = {1, 2, 3} set2 = {4, 5, 6} set3 = {3, 4, 5} print(set1.isdisjoint(set2)) # Outputs: True (set1 and set2 are disjoint) print(set1.isdisjoint(set3)) # Outputs: False (set1 and set3 are not disjoint)
In this example, set1
and set2
have no elements in common, so they are disjoint. On the other hand, set1
and set3
share the element 3
, so they are not disjoint.
Python sets provide a variety of useful methods for performing mathematical set operations such as union, intersection, difference, and symmetric difference. Additionally, methods like issubset()
, issuperset()
, and isdisjoint()
allow you to compare sets and check their relationships. By mastering these set methods, you can efficiently work with unique collections of data in Python.