Union of Sets

Tags: Set, union

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

Example 1:

Input: {1, 2, 3, 4, 5}, {4, 5, 6, 7, 8} 
Output: {1, 2, 3, 4, 5, 6, 7, 8}

Example 2:

Input: {5, 7, 8, 9, 10}, {1, 2, 3} 
Output: {1, 2, 3, 5, 7, 8, 9, 10}

Use the union() function to get all elements of two sets.

def union_of_sets(s1, s2):
    return s1.union(s2)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!