Python Set Union

Summary: in this tutorial, you’ll learn how to union two or more sets by using the Python set union() or set union operator (|).

Introduction to the set union #

The union of two sets returns a new set that contains distinct elements from both sets.

Suppose that you have the following sets:

s1 = {'Python', 'Java'}
s2 = {'C#', 'Java'}Code language: JavaScript (javascript)

The union of the s1 and s2 sets returns the following set:

{'Java','Python', 'C#'}Code language: JavaScript (javascript)

Typically, you use the Venn diagram to illustrate the union of two sets. For example: