×

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

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Python program to find the area of the triangle

Python program to find the area of the triangle This article will discuss how to find the area of a triangle in Python with all three given sides. The area of...

2 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Flutter Python

The flutter is a frame work available in the python programming language, to create web applications, mobile apps. The flutter is generally used for the development of the backend of...

3 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Introducing modern python computing in simple packages

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

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.