# Union() function in Python

## Union() function in Python

Python set Union() function returns a new set which contains all the items from the original set.

Union of two given sets is the smallest set which contains all the elements of both the sets. Union of two given sets A and B is a set which consists of all the elements of A and all the elements of B such that no element is repeated.

The symbol for denoting union of sets is ‘U’

Examples:

Input: Let set A = {2, 4, 5, 6} and set B = {4, 6, 7, 8}

Output: {2, 4, 5, 6, 7, 8}

Explanation: Taking every element of both the sets A and B, without repeating any element, we get a new set = {2, 4, 5, 6, 7, 8}.This new set contains all the elements of set A and all the elements of set B with no repetition of elements and is named as union of set A and B.

### Python set Union() Syntax:

set1.union(set2, set3, set4….)

In parameters, any number of sets can be given

### Python set Union() Return value:

The union() function returns a set, which has the union of all sets(set1, set2, set3…) with set1. It returns a copy of set1 only if no parameter is passed.

## Example of Python set Union() methods:

### Example 1: Working with Python set Union() methods

 `# Python3 program for union() function` `set1 ``=` `{``2``, ``4``, ``5``, ``6``}` `set2 ``=` `{``4``, ``6``, ``7``, ``8``}` `set3 ``=` `{``7``, ``8``, ``9``, ``10``}` `# union of two sets` `print``(``"set1 U set2 : "``, set1.union(set2))` `# union of three sets` `print``(``"set1 U set2 U set3 :"``, set1.union(set2, set3))`

Output:

```set1 U set2 :  {2, 4, 5, 6, 7, 8}
set1 U set2 U set3 : {2, 4, 5, 6, 7, 8, 9, 10}```

### Example 2: Set Union Using the | Operator

We can use “|” operator to find the union of the sets.

 `# Python3 program for union with | operator` `set1 ``=` `{``2``, ``4``, ``5``, ``6``}` `set2 ``=` `{``4``, ``6``, ``7``, ``8``}` `set3 ``=` `{``7``, ``8``, ``9``, ``10``}` `# union of two sets` `print``(``"set1 U set2 : "``, set1 | set2)` `# union of three sets` `print``(``"set1 U set2 U set3 :"``, set1 |set2 | set3)`

Output:

```set1 U set2 :  {2, 4, 5, 6, 7, 8}
set1 U set2 U set3 : {2, 4, 5, 6, 7, 8, 9, 10}```

Last Updated on December 29, 2021 by admin

## append() and extend() in Pythonappend() and extend() in Python

append() and extend() in Python Append method: Adds its argument as a single element to

## Convert JSON to CSV in PythonConvert JSON to CSV in Python

Convert JSON to CSV in Python The full form of JSON is JavaScript Object Notation. It

## Python – Count occurrences of a character in stringPython – Count occurrences of a character in string

Python | Count occurrences of a character in string Given a string, the task is

## Indexing and Selecting Data with PandasIndexing and Selecting Data with Pandas

Indexing and Selecting Data with Pandas Indexing in Pandas : Indexing in pandas means simply

## Count Values in Pandas DataframeCount Values in Pandas Dataframe

Count Values in Pandas Dataframe   In this article, we are going to count values

## Python sep parameter in print()Python sep parameter in print()

Python | sep parameter in print() The separator between the arguments to print() function in

## Python join() functionPython join() function

Python join() function The join() method is a string method and returns a string in

## Python Input Methods for Competitive ProgrammingPython Input Methods for Competitive Programming

Python Input Methods for Competitive Programming Python is an amazingly user-friendly language with the only