Javatpoint Logo
Javatpoint Logo

Union of Set in Python

Set:

The set type built-in Python includes the following features:

  • Sets are not sorted.
  • Unique elements make up a set. It is forbidden to use duplicate elements.
  • The components that make up a set must be of an immutable type; a set itself may be changed.

A Set in Python is an unsorted collection of distinct elements. You can use 'curly braces{}' or the built-in 'set() method' to build a set.

Code:

Sets can be used for a number of different actions, including adding and removing elements, determining whether an element is a part of a set, locating the union or intersection of sets, and more. These are a few instances:

Code:

Union of Set:

The set union operation in Python is used to combine two sets into a single set that contains all the unique elements from both sets. The union() method or the '|' operator are used to conduct the union operation. In this article, we will discuss how to perform set union in Python using both these methods.

Using the '|' operator:

The '|' operator is used to perform the set union operation in Python. It accepts two sets as operands and outputs a new set that is entirely composed of the elements from the input sets, with no duplicates.

Syntax:

Example: Union of Set Using '|' Operator.

Output:

Union of Set in Python

In the above example, we have two sets set1 and set2, and we perform the set union operation using the '|' operator. Without any duplications, the final set contains every element from both sets.

Using the union() method:

The set union operation in Python is carried out using the union() function. It accepts one or more sets as parameters and outputs a new set that contains each set's distinct items.

Syntax:

Example: Union of Set Using union() Method.

Output:

Union of Set in Python

In the above example, we have two sets set1 and set2, and we perform the set union operation using the union() method. There are no duplicate elements in the final set, only the unique components from both sets.

We can also perform the set union operation on more than two sets using the union() method.

Example: Let's examine how to union three sets using the set method.

Output:

Union of Set in Python

In the above example, we have three sets set1, set2, and set3, and we perform the set union operation using the union() method. Each unique element from each of the three sets is present in the final set, which is duplicate-free.

Using the update() method:

The update() method is used to add all the elements from one set to another set. This method can also be used to perform the set union operation in Python.

Syntax:

Example: Using the update() method.

Output:

Union of Set in Python

In the above example, we have two sets set1 and set2, and we perform the set union operation using the update() method. Without any duplications, the final set contains every element from both sets.

Conclusion:

To combine two or more sets into a single set that contains all of the distinct members from all of the sets, utilise Python's set union operator. The union operation can be performed using the '|' operator, the union() method, or the update() method. All of the elements from all of the sets are combined into the final set without any duplications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA