Python

Python Menu

Definition

The union() method returns a set that contains all items from the original set, and all items from the specified sets. You can specify as many sets you want, separated by commas. If an item is present in more than one set, the result will contain only one occurrence of this item.

Syntax

set.union(set1, set2...)

Parameters

Parameter Description
set1 Required. The set to unify with
set2... Optional. The other set to unify with. You can compare as many sets as you like. Separate each set with a comma.

Examples:

persons1 = {"Mark", "Jeff", "Ivan", "Mike"} persons2 = {"Arnel", "Marie", "Ivan", "Alex"} persons = persons1.union(persons2) print(persons)
# example with 3 sets persons1 = {"Mark", "Jeff", "Ivan", "Mike"} persons2 = {"Arnel", "Marie", "Ivan", "Alex"} persons3 = {"Ivan", "Cindy", "Sarah", "Igor"} persons = persons1.union(persons2, persons3) print(persons)

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods