Python Set Union With Example

In set theory, the union of a collection of sets is the set of all the elements in the collection of the sets. Following image depicts the set union operations between a collection of sets.

 

Set Union

Python Set Union

Python set class provides union() function to get the union of a collection of sets. The result is a new set with all the elements from the collection of sets.

Let’s look at some examples of Python set union() function.

Output:

Python Set Union

Union of Multiple Sets

We can create the union of multiple sets through two ways.

  1. By passing multiple sets as argument in union() function.
  2. Since union() returns a new set, we can create a chain of union() function calls.

Below code snippet shows above two ways implementation.

Output:

 

Python Multiple Sets Union

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: