×

Python Set union() method

Python Set union() method

The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets.

Syntax

set.union(set1, set2...)

Parameter

set1- This parameter represents the first set to search for equal items

set2- This argument signifies the other set to search for equal items.

One can compare as many sets as needed, only need to separate the sets with a comma.  

Return

This method returns a new set with distinct elements from all the sets and writing the common elements between both the sets only once.

Example 1

# Python program explaining
# the set.union() method
# initializing the set1
set1 = {1,2,3,4,5,6}
# printing the actual set 1
print("Set 1:",set1)
# initializing the set2 
set2 = {2,7,8,6,5}
# printing the actual set 2
print("Set 2:",set2) 
# returns a new set with different items from all the sets and 
#writing the common elements between both the sets only once
Union= set1.union(set2)
print("A U B: ",Union) 

Output

Set 1: {1, 2, 3, 4, 5, 6}
Set 2: {8, 2, 5, 6, 7}
A U B:  {1, 2, 3, 4, 5, 6, 7, 8} 

Example 2

# Python program explaining
# the set.union() method
# initializing a list of fruits
A = {'apple', 'watermelon', 'mango','guava','orange','Lichi'} 
# initializing the summer fruits
B = {'Mushmelon', 'watermelon', 'mango','Blueberry'}
# initializing the specifically the winter fruits 
C= {'apple','guava','orange','Grapes'}
# will return the unique elements in both the sets
print('A U B =', A.union(B))
print('B U C =', B.union(C)) 
#passing two set parameters in the union method
# will return the unique items from all the sets
print('A U B U C =', A.union(B, C)) 
# passing only one argument
print('A.union() = ', A.union()) 

Output

A U B = {'mango', 'apple', 'watermelon', 'Mushmelon', 'orange', 'Lichi', 'guava', 'Blueberry'}
B U C = {'mango', 'Mushmelon', 'apple', 'orange', 'guava', 'Grapes', 'watermelon', 'Blueberry'}
A U B U C = {'mango', 'apple', 'Mushmelon', 'orange', 'Lichi', 'guava', 'Grapes', 'watermelon', 'Blueberry'} 
 A.union() =  {'mango', 'apple', 'watermelon', 'orange', 'Lichi', 'guava'} 

Example 3

# Python program explaining
# the set.union() method
A = {1,2,3,4,5,6}
B = {2,4,6,8 } 
print("Printing the union by two different method>>>")
# the operator '|' is used to get the union for two sets
print("By '|' operator\n A U B:", A | B)
# using the set.union() method 
print("Bu using the set.union() method\n A U B:",A.union(B)) 

Output

Printing the union by two different method>>>
By '|' operator
A U B: {1, 2, 3, 4, 5, 6, 8}
Bu using the set.union() method
A U B: {1, 2, 3, 4, 5, 6, 8} 

Related Topics

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.