×

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 String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

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.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python Typing Module

An Introduction to the Typing Module The typing module is introduced in Python version 3.5 and used to provide hinting method types in order to support static type checkers and linters...

10 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python String islower() method

Python String islower() method The string.islower() method returns a boolean value true if all cased characters in the string are lowercase and for  any other value is returns false otherwise. Syntax string.islower() Parameter NA Return This...

1 minute read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

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.

Reason for Python So Popular

One of the languages that is witnessing outstanding development and acceptance every year in Python. Python has emerged as the programming language with the greatest rate of growth, and Stack...

3 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.