Python – Join Sets

In Python, joining sets means combining two or more sets together to form a new set. Since sets are unordered and do not allow duplicates, the resulting set will contain only unique elements from all the sets involved.

Here are the common ways to join sets:

1. Using the union() Method

The union() method returns a new set that contains all elements from both sets, removing duplicates.

Syntax:

set1.union(set2)

Example:

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Join two sets using union()
result_set = set1.union(set2)

print("Joined set using union:", result_set)

Output:

Joined set using union: {1, 2, 3, 4, 5}

2. Using the | (Pipe) Operator

The | operator is another way to join sets. It performs the same operation as union(), combining the sets and removing duplicates.

Syntax:

set1 | set2

Example:

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Join two sets using the | operator
result_set = set1 | set2

print("Joined set using | operator:", result_set)

Output:

Joined set using | operator: {1, 2, 3, 4, 5}

3. Using the update() Method

The update() method adds all elements from another set (or any iterable) to the set it’s called on. This modifies the original set and does not return a new set.

Syntax:

set1.update(set2)

Example:

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Join set2 into set1
set1.update(set2)

print("Set1 after update:", set1)

Output:

Set1 after update: {1, 2, 3, 4, 5}

Note that update() modifies set1 in place, so no new set is created.


4. Joining Multiple Sets

You can join more than two sets using union(), |, or update() by chaining them.

Example using union():

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}

# Join three sets using union()
result_set = set1.union(set2).union(set3)

print("Joined sets using multiple union:", result_set)

Output:

Joined sets using multiple union: {1, 2, 3, 4, 5, 6, 7}

Example using | (Pipe) operator:

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}

# Join three sets using | operator
result_set = set1 | set2 | set3

print("Joined sets using | operator:", result_set)

Output:

Joined sets using | operator: {1, 2, 3, 4, 5, 6, 7}

5. Using update() with Multiple Sets

You can use the update() method to add all elements from multiple sets to a base set.

Example:

# Given sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}

# Update set1 with set2 and set3
set1.update(set2, set3)

print("Set1 after multiple updates:", set1)

Output:

Set1 after multiple updates: {1, 2, 3, 4, 5, 6, 7}

Summary of Ways to Join Sets:

  • union(): Returns a new set containing all unique elements from both sets.
  • | (Pipe operator): A shorthand for union(), combining the sets.
  • update(): Adds elements from another set to the original set in place (modifies the original set).
  • Multiple Sets: You can join multiple sets by chaining union(), |, or using update() with multiple sets.
Leave a Reply 0

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