×

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 Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 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.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Python String Lowercase

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.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

How to Update Python?

How to Update Python In this article, we will discuss how we can update Python in our system. For a better understanding, this article will cover all the steps right from installation...

4 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.