Set Operation in Python
- Get link
- X
- Other Apps
Set Operation in Python
In Python, sets are a powerful data structure that allow you to perform various set operations to manipulate data. A set is an unordered collection of unique elements, and it supports operations such as union, intersection, difference, and symmetric difference. In this blog post, we will explore each of these set operations in Python.
Creating Sets in Python
In Python, a set can be created by enclosing a list of elements within curly braces {}
or by using the set()
function. Here are some examples:
# creating a set using curly braces set1 = {1, 2, 3, 4, 5} # creating a set using the set() function set2 = set([4, 5, 6, 7, 8])
Set Union Operation
The union of two sets set1
and set2
is a new set that contains all the elements that are present in either set1
or set2
. In Python, the union of two sets can be obtained using the union()
method or the |
operator. Here's an example:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} # using the union() method union_set = set1.union(set2) print(union_set) # output: {1, 2, 3, 4, 5, 6, 7, 8} # using the | operator union_set = set1 | set2 print(union_set) # output: {1, 2, 3, 4, 5, 6, 7, 8}
Set Intersection Operation
The intersection of two sets set1
and set2
is a new set that contains only the elements that are present in both set1
and set2
. In Python, the intersection of two sets can be obtained using the intersection()
method or the &
operator. Here's an example:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} # using the intersection() method intersection_set = set1.intersection(set2) print(intersection_set) # output: {4, 5} # using the & operator intersection_set = set1 & set2 print(intersection_set) # output: {4, 5}
Set Difference Operation
The difference of two sets set1
and set2
is a new set that contains only the elements that are present in set1
but not in set2
. In Python, the difference of two sets can be obtained using the difference()
method or the -
operator. Here's an example:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} # using the difference() method difference_set = set1.difference(set2) print(difference_set) # output: {1, 2, 3} # using the - operator difference_set = set1 - set2 print(difference_set) # output: {1, 2, 3}
Set Symmetric Difference Operation
In Python, the symmetric difference of two sets set1
and set2
is a new set that contains only the elements that are present in either set1
or set2
, but not in both. In other words, it is the set of all elements that are in either of the sets, but not in their intersection.
The symmetric difference can be obtained using the symmetric_difference()
method or the ^
operator. Here's an example:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} # using the symmetric_difference() method symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # output: {1, 2, 3, 6, 7, 8} # using the ^ operator symmetric_difference_set = set1 ^ set2 print(symmetric_difference_set) # output: {1, 2, 3, 6, 7, 8}
set1
and set2
contains the elements {1, 2, 3, 6, 7, 8}
. Note that this set includes all the elements that are present in either set1
or set2
, but not in both.Set Operations with Multiple Sets
In Python, you can perform set operations with more than two sets by chaining the methods or operators together. Here's an example:
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = {3, 4, 5, 6} # using the union() method with three sets union_set = set1.union(set2, set3) print(union_set) # output: {1, 2, 3, 4, 5, 6, 7, 8} # using the intersection() method with three sets intersection_set = set1.intersection(set2, set3) print(intersection_set) # output: {4, 5} # using the difference() method with three sets difference_set = set1.difference(set2, set3) print(difference_set) # output: {1, 2} # using the symmetric_difference() method with three sets symmetric_difference_set = set1.symmetric_difference(set2, set3) print(symmetric_difference_set) # output: {1, 2, 6, 7, 8}
In the above example, we used the union()
, intersection()
, difference()
, and symmetric_difference()
methods with three sets set1
, set2
, and set3
.
Conclusion
In Python, sets are a powerful data structure that support various set operations, including union, intersection, difference, and symmetric difference. These operations can be performed using the respective methods or operators provided by the set data type. Understanding these set operations can help you manipulate sets effectively and efficiently in your Python programs.
Happy Learning!! Happy Coding!!
- Get link
- X
- Other Apps
Comments