In the last article, we saw how we can find the difference between 2 sets in Python. In this article, we are going to see how to find the union of 2 sets.
This is actually quite similar to the difference.
We also have 2 ways to do the union of 2 sets in Python.
Let us say that we have 2 sets like the following:
If we want to find their union, we can simply use the *union()* method like the following:
We can also use the same method to find the union of 2 lists:
In fact, we can find the union of 2 lists even by using the plus sign, as shown below:
This plus operator only works for lists, but not for sets.
However, there is another sign that works for the union of sets: the pipe operator:
This only works for sets and not for lists.
That’s basically it.
I hope you find it useful.