Python Set Union with Advanced Examples

In this tutorial, you will learn the Python Set Union method. It is a set of methods in which all items from the first set and all items from the second set are included. You can provide as many sets as you want, as long as they are separated by commas.

In addition, this set can be iterable over any object. Iterable means an object that can be looped over (iterated) using a for loop.

Also read: Easy Way to Check if Directory Exists Python with Example

What is the Python Set union()?

In Python, the Set Union() method allows elements to create a new set of elements from all the sets which give values from both sets. The union() method, also known as the set union operator (|), is designed to join two or more sets together.

Syntax:

set1.union(set2, set3, set4….)

Parameters:

Zero or More Sets 

Return value from a set union()

  • The union method sets new elements from the set and all other sets (passed as an argument).
  • The union () method returns a shallow copy of the set if the argument is not passed to it. Shallow means lacking in depth or solidity.

Set union() in Python method advanced example:

g = {"j", "d", "s"}
l = {"a", "d", "n"}
n = {"c", "r", "b"}

result = g.union(l, n)

print(result)

Output:

{'s', 'b', 'a', 'r', 'n', 'd', 'c', 'j'}

Working of set union

In Python, union() takes two or more sets and returns all the unique elements that are in all of the sets.

For example;

A = {1, 2}
B = {2, 3, 4}
C = {5}

Then,
A∪B = B∪A = {1, 2, 3, 4}
A∪C = C∪A = {1, 2, 5}
B∪C = C∪B = {2, 3, 4, 5}

A∪B∪C = {1, 2, 3, 4, 5}

Set Union Using the | Operator

In Python, union() sets using the | operator only accept sets. While the union method accepts one or more iterables.

For example;

G = {'g', 'l', 'n'}
L = {'n', 'h', 3 }
N = {2, 4, 6}

print('G U L =', G | L)
print('L U N =', L | N)
print('G U L U N =', G | L | N)

Output:

G U L = {3, 'h', 'l', 'g', 'n'}
L U N = {2, 3, 'h', 4, 6, 'n'}
G U L U N = {2, 3, 'h', 4, 6, 'l', 'g', 'n'}

Why union is used in Python?

The union() method is used to return a new set with all the items from the first set and all of the items from the second set (s). You can list as many sets as you want, with commas between each one.

It doesn’t have to be a set, it can be any object that can be iterated over.

Python union list of sets

The following lists are the strategies to union a list of sets.

  • The first step is to use the set() constructor to make a new set.
  • Second, you can call the function union method on the new set of objects.
  • Third, use the asterisk operator *list to unpack the list and pass all the sets as arguments to the union() method.
  • Last, the union() method creates a new set with all of the elements that are in at least one of the original sets.

What is union symbol in Python?

In Python, the union symbol is represented by a ∪ symbol. This union Python built-in function set union() is exactly the same as the union operation in mathematics.

For example;

G = {2, 4, 6}

L = {8, 10, 12}

G U L = {2, 4, 6, 8, 10, 12}  (Union of Set G and Set L)

How to find the union of two sets in Python?

In Python, to find the union of sets with two or more sets, use the union () method new_set = set.

What is structure and union in Python?

The Structure is mostly used to store different types of data, while union is mostly used to store just one type of data. You can get any member at once from a structure with distinct elements.

On the other hand, you can only talk to one member at a time in union. The structure supports a flexible array and data types, but the union does not.

I hope this tutorial has helped you learn a lot. Check out our website and see our previous and latest articles for more life-changing tutorials that could help you a lot.

Leave a Comment