Python Set union() Method: Combine Multiple Sets into One | Syntax, Examples & Use Cases

Introduction: Python Set union() Method

In Python, sets are widely used when you want to store unique values, and often you may need to combine multiple sets into a single collection.

Instead of manually merging values or handling duplicates yourself, Python provides a direct way to do this using the Python Set union() method.

What it is: The union() method is a built-in Python set method used to combine two or more sets into a single set containing all unique elements.

In simple terms, it merges multiple sets while automatically removing duplicates and keeping only one copy of each value.

The original sets remain unchanged, and the result is always returned as a new set.

Check the quick example below.

Check the use cases below.

Let’s first understand its syntax before moving into real examples and use cases.

Tip: Explore union and related techniques in the Python Sets operations guide.

Syntax, Parameters, Return Values and Examples: Python Set union() Method

Before using the Python set union() method, let’s understand its syntax, parameters, return value, and basic working process.

Syntax

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

Explanation

  • It combines two or more sets into one result.
  • Duplicate values are automatically removed.
  • The original sets are not modified.

Parameters

Parameter Description
set2, set3, … One or more sets or iterable objects that will be combined with the first set (set1)

Return Value

Return Type Description
set A new set containing all unique elements from the given sets.

Quick Example

A simple example showing how the union() method combines two sets and returns a new set containing all unique elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.union(set2))

Output:

{1, 2, 3, 4, 5}

The output contains all unique values from both sets. The duplicate value 3 is automatically removed because sets do not allow duplicate elements.

How set Python union() method works

  • The union() method combines two or more sets into a single set by merging all their values together.
  • If a value appears in more than one set, it is still included only once because sets store unique elements.
  • The original sets are not modified during the process.
  • Instead, Python returns a new set containing all unique values from the given sets.

Practical Examples: Set union() Method

Now that the basics of the Python set union() method are clear, let’s explore different practical examples to understand how it combines unique elements from multiple sets.

Simple Level Examples

Example 1: Merge two sets

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.union(set2))

# Output (output may vary):
{1, 2, 3, 4, 5}

Explanation: The union() method combines elements from both sets into one set. Since the value 3 exists in both sets, it appears only once in the output.

Example 2: Merge three sets

a = {"apple", "banana"}
b = {"banana", "cherry"}
c = {"cherry", "date"}

print(a.union(b, c))


# Output (output may vary):
{'apple', 'banana', 'cherry', 'date'}

Explanation: All three sets are merged together, and duplicate values such as “banana” and “cherry” are stored only once.

Medium Level Examples

Example 3: Union with another iterable

numbers = {1, 2, 3}
result = numbers.union([3, 4, 5])
print(result)


# Output (output may vary):
{1, 2, 3, 4, 5}

Explanation: The union() method can combine a set with another iterable such as a list. The duplicate value 3 is included only once in the final result.

High Level Examples

Example 6: Survey data merging

survey_group1 = {"yes", "no"}
survey_group2 = {"no", "maybe"}
survey_group3 = {"yes", "maybe"}

all_responses = survey_group1.union(survey_group2, survey_group3)
print(all_responses)


# Output (output may vary):
{'yes', 'no', 'maybe'}

Explanation: Responses from multiple survey groups are merged into one set, while repeated answers are automatically stored only once.

Use Cases: When to use union() method in Python set

Now let’s understand where Python Set union() method is actually used in real Python programs:

  • Merging data from multiple sources into a single set for unified processing.
  • Combining different categories or groups for analysis and reporting.
  • Collecting all unique values across multiple datasets without duplicates.
  • Preparing clean, duplicate-free data during merge operations.

Key Takeaways: Set union() Method

The following points summarize the important concepts and usage of the Python set union() method:

  • The union() method combines two or more sets into a single set.
  • Only unique values are included in the final result.
  • The original sets are not modified during the operation.
  • It can handle multiple sets in a single function call.
  • It is commonly used for merging data and removing duplicates.

In short, the Python set union() method provides a simple way to merge multiple datasets while keeping the result clean and duplicate-free.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top