Python Program for Set union() Method

In the previous article, we have discussed Python Program for Dictionary popitem() Method
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given First set = {'hello', 'this', 'is', 'btechgeeks'}
Given second set = {'good', 'morning', 'btechgeeks'}

Output:

The given first set is :
{'this', 'hello', 'btechgeeks', 'is'}
The given second set is :
{'btechgeeks', 'morning', 'good'}
The union set for the given first and second sets is :
{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}

Explanation:

Here 'hello' is repeated in the both the sets. so, the union() method combines both the sets but prints 
'hello' only once(i.e, removes duplicates and writes only once )

Example2:

Input:

Given First set = {2, 4, 6, 8, 0, 2, 1}
Given second set = {3, 1, 4, 4, 0, 2, 5, 1}

Output:

The given first set is :
{0, 1, 2, 4, 6, 8}
The given second set is :
{0, 1, 2, 3, 4, 5}
The union set for the given first and second sets is :
{0, 1, 2, 3, 4, 5, 6, 8}

Program for Set union() Method in Python

set union() Method:

The union() method returns a set containing all of the items from the original set as well as all of the items from the specified set (s).

You may enter as many sets as you like, separated by commas.

It does not have to be a set; any iterable object can be used.

If an item appears in more than one set, the result will only contain one instance of that item. (i.e, appears only once)

Syntax:

set.union(set1, set2........)

Parameters:

set1: This is required. The iterable to combine with. (union)

set2: This is Optional.The other iterable with which to combine. You are free to compare as many iterables as you want. Use a comma to separate each iterable.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and initialize it with some random values.
  • Store it in a variable.
  • Give the second set as static input and initialize it with some random values.
  • Store it in another variable.
  • Print the above-given first set.
  • Print the above-given second set.
  • Apply union() method to the given first and second set which combines both the sets and removes the duplicate items.
  • Store it in another variable.
  • Print the union set for the given first and second sets.
  • The Exit of Program.

Below is the implementation:

# Give the first set as static input and initialize it with some random values.
# Store it in a variable.
fst_sett = {'hello', 'this', 'is', 'btechgeeks'}
# Give the second set as static input and initialize it with some random values.
# Store it in another variable.
scnd_sett = {'good', 'morning', 'btechgeeks'}
# Print the above-given first set.
print("The given first set is :")
print(fst_sett)
# Print the above-given second set.
print("The given second set is :")
print(scnd_sett)
# Apply union() method to the given first and second set which combines both the
# sets and removes the duplicate items.
# Store it in another variable.
unionn_sett = fst_sett.union(scnd_sett)
# Print the union set for the given first and second sets.
print("The union set for the given first and second sets is :")
print(unionn_sett)

Output:

The given first set is :
{'this', 'hello', 'btechgeeks', 'is'}
The given second set is :
{'btechgeeks', 'morning', 'good'}
The union set for the given first and second sets is :
{'morning', 'is', 'good', 'this', 'hello', 'btechgeeks'}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using the set(), map(), input(), and split() functions.
  • Store it in a variable.
  • Give the second set as user input using the set(), map(), input(), and split() functions.
  • Store it in another variable.
  • Print the above-given first set.
  • Print the above-given second set.
  • Apply union() method to the given first and second set which combines both the sets and removes the duplicate items.
  • Store it in another variable.
  • Print the union set for the given first and second sets.
  • The Exit of Program.

Below is the implementation:

# Give the first set as user input using the set(), map(), input(), and split() functions.
# Store it in a variable.
fst_sett = set(map(int,input("Enter some random values separated by spaces = ").split()))
# Give the second set as user input using the set(), map(), input(), and split() functions.
# Store it in another variable.
scnd_sett = set(map(int,input("Enter some random values separated by spaces = ").split()))
# Print the above-given first set.
print("The given first set is :")
print(fst_sett)
# Print the above-given second set.
print("The given second set is :")
print(scnd_sett)
# Apply union() method to the given first and second set which combines both the
# sets and removes the duplicate items.
# Store it in another variable.
unionn_sett = fst_sett.union(scnd_sett)
# Print the union set for the given first and second sets.
print("The union set for the given first and second sets is :")
print(unionn_sett)

Output:

Enter some random values separated by spaces = 45 55 65 75 85
Enter some random values separated by spaces = 28 45 65 20 10 30
The given first set is :
{65, 75, 45, 85, 55}
The given second set is :
{65, 10, 45, 20, 28, 30}
The union set for the given first and second sets is :
{65, 10, 75, 45, 20, 85, 55, 28, 30}

Well organized and easy to understand tutorials of us containing Python Set Methods Examples will help you work effectively on Python Set Methods.