How to Use Set union() Method in PythonPython Basics


The union() method returns a new set with distinct elements from all the other sets.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

A = {1, 2, 3, 4}
B = {1, 2 ,4,5,6,7,9}
C = {1, 2,11,3}

print("The union between set A and set B is",A.union(B))

print("The union between set A and set C is",A.union(C))

print("The union between set C and set B is",B.union(C))

Output:

The union between set A and set B is {1, 2, 3, 4, 5, 6, 7, 9}
The union between set A and set C is {1, 2, 3, 4, 11}
The union between set C and set B is {1, 2, 3, 4, 5, 6, 7, 9, 11}

Syntax

A.union(*other_sets)

Parameters

The input parameter is another set.

Return Value

It returns the union of a set with all the other sets.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us