When working with data, you might frequently wish to save a list of data that cannot contain duplicate entries and whose values cannot be modified. This is where the Python set operations and its various objects comes into play. A set is a changeable object that contains an unordered collection of things with only unique values. The set structure is valuable because it includes a variety of mathematical operations that may be used to work with the data stored in a set. A set is formed by enclosing all of the items (elements) behind curly brackets {}, separated by commas, or by using the built-in set() function. This Python article will teach you how to do various set operations such as union, intersection, difference, and symmetric difference.
Python Set Operations
Sets can be used to perform set operations such as union, intersection, difference, and symmetric difference. This is something we can achieve using operators or procedures. Because these are some uncommon Python set operations, we’ll go over them in depth.
Set Union Operation
This Python set operation performs a Union of 2 functions i.e. it is the set of all the elements from both the sets in a single different set. The | operator is used to perform union. The union() method can be used to do the same thing. If similar elements occur in both the sets, then their single value is considered.
Example
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set union operation
print('Union of A and B:', A | B)
Output
Union of A and B: {0, 1, 2, 3, 4, 5, 6, 7, 9}
Set Intersection Operation
The intersection of two sets is a set of elements that are shared by both sets i.e. generate a list of elements that are common between two sets. The & operator is used to perform intersection. The intersection() method can be used to do the same thing.
Example
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set intersection operation
print('Intersection of A and B:', A & B)
Output
Intersection of A and B: {9, 2, 7}
Set Difference Operation
The difference between sets B and A (i.e. A-B) is a set of elements that are only in A but not in B. The – operator is used to perform a difference. The difference() method can be used to do the same thing. The difference() method can be used to determine which elements are present in one set but not in another.
Example
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set difference operation
print('Difference of A and B:', A - B)
Output
Difference of A and B: {1, 3, 5}
Set Symmetric Difference Operation
A and B Symmetric Difference is a set of items in A and B but not in both (excluding the intersection). The operator is used to perform symmetric difference. The same thing can be done with the symmetric_difference() method.
Example
# Python program to perform different set operations
# defining 2 sets
A = {1, 2, 3, 5, 7, 9}
B = {2, 4, 6, 7, 9, 0}
# set symmetric difference operation
print('Symmetric Difference of A and B:', A ^ B)
Output
Symmetric Difference of A and B: {0, 1, 3, 4, 5, 6}
The resulting set contains elements that are distinct to each set. If an element is in the left-hand set but not in the right-hand set, or if it is in the right-hand set but not in the left-hand set, it will be in the result set.
Leave a Reply