Python Practice › Set Programs › Program 6

Union of two sets

EasySet Programs · 6 of 48

Write a Python program to find the union of two sets.

Advertisement
Input
{1, 2, 3}, {3, 4, 5}
Output
{1, 2, 3, 4, 5} {1, 2, 3, 4, 5}

Approach

  1. | or union().

Solution

Python
a, b = {1, 2, 3}, {3, 4, 5}print(a | b, a.union(b))
Output
{1, 2, 3, 4, 5} {1, 2, 3, 4, 5}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs