Python Join 2 Sets

Today we will discuss Python sets join with examples, in hopes that it will meet our learning objectives.

Join Two Sets

It is possible to join two or more Python sets in several ways when we talk about Python sets join.

The union() method returns a new set that contains all items from both sets, or the update() method inserts all items from one set into the other.

In the union() method, the items from both sets are combined into a new set:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {1, 2 , 3, 4, 5} current_set = country_set.union(number_set) print(current_set)

A set2 item is inserted into a set1 item using the update() method:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {1, 2 , 3, 4, 5} country_set.update(number_set) print(country_set)

Reminder: Duplicate items will be excluded from both the union() and update() methods.



Keep Only Duplicates

When it comes to Python sets join, intersection_update() method keeps only items that appear in both sets.

Keeping both set country_set and set country_set1 items:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {"Australia","United States of America", "Ireland","United Kingdom"} country_set.intersection_update(number_set) print(country_set)

By calling the intersection() method, a new set is returned that contains only items that are present in both sets.

The following set contains all items from country_set and country_set1:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {"Australia","Thailand", "Ireland"} current_set = country_set.intersection(number_set) print(current_set)

Keep All Except Duplicates

A Python sets join uses the symmetric_difference_update() method to keep only those elements that are not present in both sets.

The items that are not included in both sets should be kept:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {"Australia","Thailand", "Ireland"} country_set.symmetric_difference_update(number_set) print(country_set)

As a result of using the symmetric_difference() method, a new set will be returned that only contains the elements that are not present in both sets.

You will receive a set containing all items from both sets except those that are present in both sets:

Example: 

country_set = {"United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy"} number_set = {"Australia","Thailand", "Ireland"} current_set = country_set.symmetric_difference(number_set) print(current_set)

Python Join Set Methods Importance

The following are the uses of the set join methods:

  1. The add() method is used to add an element to a set. It ensures that the element is unique within the set, preventing duplicates. This is particularly useful when you want to maintain a collection of unique elements.
  2. The remove() method allows you to remove a specific element from a set. It is helpful when you know the exact element you want to eliminate. However, it raises a KeyError if the element is not found in the set, so it’s important to ensure the element exists before removing it.
  3. The clear() method is used to remove all elements from a set, effectively emptying it. It provides a quick and straightforward way to reset or reuse a set without having to recreate it.
  4. The union() method returns a new set that contains all the unique elements from both the original set and set2. It is useful when you want to combine two sets while ensuring that there are no duplicate elements.
  5.  The intersection() method returns a new set that contains only the common elements between the original set and set2. It allows you to find the shared elements between two sets, which can be helpful in various scenarios such as finding common elements among multiple sets or checking for overlapping values.

 

Subscribe to our Newsletter and stay up to date with the latest technical information.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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