Python - Join Sets and its different types of methods with code and real-world examples

  

Join Sets

There are several ways to join two or more sets in Python.

The union() and update() methods join all items from both sets.

The intersection() method keeps ONLY the duplicates.

The difference() method keeps the items from the first set that are not in the other set(s).

The symmetric_difference() method keeps all items EXCEPT the duplicates.

the union() and update() methods in Python in a simple way. These methods are commonly used with sets, which are unordered collections of unique elements.

  1. union():

The union() method combines two or more sets, creating a new set containing all unique elements from all the sets.

Think of it like mixing different colored marbles from separate bags into a new bag, but only keeping one of each color:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} combined_set = set1.union(set2) print(combined_set) # Output: {1, 2, 3, 4, 5}

Key points:

  • It doesn't modify the original sets.
  • It returns a new set.
  • Duplicate elements are included only once.
  1. update():

The update() method adds elements from another set (or any iterable) to an existing set.

Imagine adding new marbles to your existing bag, but again, only keeping one of each color:

python
set1 = {1, 2, 3} set2 = {3, 4, 5} set1.update(set2) print(set1) # Output: {1, 2, 3, 4, 5}

Key points:

  • It modifies the original set.
  • It doesn't return a new set; it changes the set in place.
  • Duplicate elements are still included only once.

The main differences:

  • union() creates a new set; update() modifies an existing set.
  • union() can be used with the | operator; update() uses the |= operator.

Both methods achieve similar results, but union() keeps the original sets unchanged, while update() modifies one of the sets.

intersection() method:

The intersection() method is used with sets in Python. It finds the common elements between two or more sets.

Imagine you have two circles that overlap:

Set A Set B ___ ___ / \ / \ / ___|___/ \ | / | \ | | | | | | \ \__|__/ / \___/ \___/ Intersection

The intersection() method gives you the part where these circles overlap - the elements that exist in both sets.

Here's a simple example:

python
fruits1 = {"apple", "banana", "cherry"} fruits2 = {"cherry", "orange", "banana"} common_fruits = fruits1.intersection(fruits2) print(common_fruits) # Output: {'cherry', 'banana'}

In this case:

  • fruits1 has apple, banana, and cherry
  • fruits2 has cherry, orange, and banana
  • The intersection() finds that banana and cherry are in both sets

Key points about intersection():

  1. It returns a new set with the common elements.
  2. It doesn't change the original sets.
  3. If there are no common elements, it returns an empty set.
  4. You can use it with more than two sets to find elements common to all of them.

You can also use the & operator as a shorthand for intersection:

python
common_fruits = fruits1 & fruits2

This does the same thing as intersection().

In everyday terms, you could think of intersection() as finding what things different groups agree on, or what items appear on multiple shopping lists.

difference() method in Python :

The difference() method is used with sets in Python. It finds the elements that are in one set but not in the other.

Imagine you have two circles that partially overlap:

Set A Set B ___ ___ / \ / \ / \___/ \ | A-B | | B-A | | | | | \ /---\ / \___/ \___/ Difference (A-B)

The difference() method gives you the part of the first circle (Set A) that doesn't overlap with the second circle (Set B). It's like asking, "What does Set A have that Set B doesn't?"


Here's a simple example:

python
fruits1 = {"apple", "banana", "cherry"} fruits2 = {"banana", "orange", "kiwi"} unique_to_fruits1 = fruits1.difference(fruits2) print(unique_to_fruits1) # Output: {'apple', 'cherry'}

In this case:

  • fruits1 has apple, banana, and cherry
  • fruits2 has banana, orange, and kiwi
  • The difference() finds that apple and cherry are in fruits1 but not in fruits2

Key points about difference():

  1. It returns a new set with the elements that are unique to the first set.
  2. It doesn't change the original sets.
  3. The order matters: A.difference(B) is not the same as B.difference(A).
  4. You can use the - operator as a shorthand for difference: fruits1 - fruits2.

In everyday terms, you could think of difference() as finding what items are on your shopping list that aren't on your friend's shopping list.

symmetric_difference() method in Python:

The symmetric_difference() method is used with sets in Python. It finds elements that are in either of the sets, but not in both. In other words, it gives you all the unique elements from both sets.

Imagine you have two circles that partially overlap:

Set A Set B ___ ___ / \ / \ / ___|___/ \ | / | \ | | | | | | \ \__|__/ / \___/ \___/ Symmetric Difference (shaded areas)

The symmetric_difference() method gives you the parts of both circles that don't overlap. It's like asking, "What elements are unique to each set?"

Here's a simple example:

python
fruits1 = {"apple", "banana", "cherry"} fruits2 = {"banana", "orange", "kiwi"} unique_fruits = fruits1.symmetric_difference(fruits2) print(unique_fruits) # Output: {'apple', 'cherry', 'orange', 'kiwi'}

In this case:

  • fruits1 has apple, banana, and cherry
  • fruits2 has banana, orange, and kiwi
  • The symmetric_difference() finds:
    • apple and cherry (unique to fruits1)
    • orange and kiwi (unique to fruits2)
    • banana is in both, so it's not included in the result

Key points about symmetric_difference():

  1. It returns a new set with elements that are in either set, but not in both.
  2. It doesn't change the original sets.
  3. The order doesn't matter: A.symmetric_difference(B) is the same as B.symmetric_difference(A).
  4. You can use the ^ operator as a shorthand: fruits1 ^ fruits2.

In everyday terms, you could think of symmetric_difference() as finding what items are on either your shopping list or your friend's shopping list, but not on both lists.



Comments