Sets Methods In Python

In today’s topic, Python sets methods are discussed with examples, hoping to meet the learning needs.

Python Sets Methods



Python Sets Methods

When it comes to Python sets methods, Python comes with a number of built-in methods.

MethodOverview
add()A Python set method adds an item to the set.
clear()This function removes all the items from a set.
copy()This method returns a copy of the set.
difference()Provides a set including the difference between two or more sets.
difference_update()This set is removed if it contains items that are included in another set mentioned in this one.
discard()Delete the chosen item.
intersection()Provides a set whose intersection is two other sets.
intersection_update()This set should be removed if it contains items not found in other specified sets.
isdisjoint()A function that returns whether or not two sets intersect.
issubset()This method returns whether or not another set contains this set.
issuperset()This method returns whether the set includes another set or not.
pop()Delete an item from the set.
remove()Delete the chosen item.
symmetric_difference()This function returns a set with two sets that are symmetrically different.
symmetric_difference_update()Add the symmetric differences between this set and another.
union()This method returns a set containing the union of all sets.
update()Combine this set with others and update the set.

In order to make it easier for you, we have defined each Python sets methods in detail with examples.


Set add() Method

A new element is added to the set with the add() method.

The add() method does not add the element if it already exists.

Syntax

set.add(element)

 

ParameterDescription
elementIt is essential. The element is assigned to the set

Include an element to the nfl_teams set:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams.add("New York Giants") print(nfl_teams)

Add two elements to the nfl_teams set:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams.add("New York Giants") nfl_teams.add("Minnesota Vikings") print(nfl_teams)

Now add an existing element:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams.add("Chicago Bears") print(nfl_teams)

Add an existing element then add a new element in a set:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams.add("Chicago Bears") nfl_teams.add("New York Giants") print(nfl_teams)

 


Set clear() Method

By calling clear() method, all elements in a set are removed.

Syntax

set.clear()

 

Delete all elements from the nfl_teams set:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams.clear() print(nfl_teams)

Delete all elements from the nfl_teams1 and nfl_teams2 set:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Philadelphia Eagles", "San Francisco 49ers", "Houston Texans"} nfl_teams1.clear() nfl_teams2.clear() print(nfl_teams1) print(nfl_teams2)

Set copy() Method

Sets are copied through the copy() method.

Syntax

set.copy()

Copy all the elements of the nfl_teams set:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} mrx = nfl_teams.copy() print(mrx)

In the nfl_teams1 and nfl_teams2 sets, copy all the elements:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Philadelphia Eagles", "San Francisco 49ers", "Houston Texans"} mrx = nfl_teams1.copy() ample = nfl_teams2.copy() print(mrx) print(ample)

Set difference() Method

Difference() generates a set containing the difference between two sets.

As a result, the returned set contains only items from the first set and not from the second set.

Syntax

set.difference(set)

Parameter Values

ParameterDescription
setIt is essential. The set compares for differences in

Provide a set that includes items that only occur in set nfl_teams1, but not in set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams1.difference(nfl_teams2) print(mrx)

Return a set that includes items that only occur in set nfl_teams1, but not in sets nfl_teams2 and nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams1.difference(nfl_teams2, nfl_teams3) print(mrx)

You can reverse the first example. Provide a set that includes items that only occur in set nfl_teams2, but not in set nfl_teams1:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams2.difference(nfl_teams1) print(mrx)

You can also reverse the second example. Return a set that includes items that only occur in set nfl_teams3, but not in sets nfl_teams1 and nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams3.difference(nfl_teams1, nfl_teams2) print(mrx)

Set difference_update() Method

Difference_update() eliminates items that are present in both sets.

It differs from the difference() method in that the difference() method returns a new set without the unwanted items, while the difference_update() method removes them.

Syntax

set.difference_update(set)

Parameter Values

ParameterDescription
setIt is essential. The set compares for differences in

Delete the items that are present in both sets:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams1.difference_update(nfl_teams2) print(nfl_teams1)

Eliminate the elements that are found in both three sets:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} nfl_teams1.difference_update(nfl_teams2, nfl_teams3) print(nfl_teams1)

Set discard() Method

The discard() method eliminates the given item from the set.

There is a difference between the discard() method and the remove() method in that the remove() method will raise an error if the specified item does not exist, but the discard() method will not.

Syntax

set.discard(value)

Parameter Values

ParameterDescription
setIt is essential. To delete the item, search for it and remove it

From the nfl_teams set, delete “Atlanta Falcons”:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}nfl_teams.discard("Atlanta Falcons") print(nfl_teams)

From the nfl_teams set, delete “Atlanta Falcons” and “Arizona Cardinals”:

Example: 

nfl_teams = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}nfl_teams.discard("Atlanta Falcons") nfl_teams.discard("Arizona Cardinals") print(nfl_teams)

Set intersection() Method

If two or more sets are similar, the intersection() method returns their similarity.

When comparing more than two sets, the returned set only contains items that are present in both sets.

Syntax

set.intersection(set1, set2 ... etc)

Parameter Values

ParameterDescription
set1This is essential. It looks for items that are equal in the set
set2A choice. Make a search for equal items in the other set. You can compare as many sets as you like. Use a comma to separate the sets.

Provide a set that includes items that occur in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams1.intersection(nfl_teams2) print(mrx)

Return a set that includes items that occur in both set nfl_teams1, set nfl_teams2 and nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams1.intersection(nfl_teams2, nfl_teams3) print(mrx)

You can reverse the first example. Provide a set that includes items that occur in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams2.intersection(nfl_teams1) print(mrx)

You can reverse the second example. Return a set that includes items that occur in both set nfl_teams1, set nfl_teams2 and nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} mrx = nfl_teams3.intersection(nfl_teams1, nfl_teams2) print(mrx)

Set intersection_update() Method

In the intersection_update() method, items that are not present in both sets are removed (or all sets in the case of a comparison of more than two sets).

In contrast to the intersection() method, the intersection_update() method removes unwanted items from the original set, whereas the intersection() method returns a new set.

Syntax

set.intersection_update(set1, set2 ... etc)

Parameter Values

ParameterDescription
set1This is essential. It looks for items that are equal in the set
set2A choice. Make a search for equal items in the other set. You can compare as many sets as you like. Use a comma to separate the sets.

Delete the elements that are not present in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams1.intersection_update(nfl_teams2) print(nfl_teams1)

Delete the elements that are not present in both set nfl_teams1, set nfl_teams2 and set nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} nfl_teams1.intersection(nfl_teams2, nfl_teams3) print(nfl_teams1)

You can reverse the first example. Delete the elements that are not present in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams2.intersection_update(nfl_teams1) print(nfl_teams2)

You can reverse the second example. Delete the elements that are not present in both set nfl_teams1, set nfl_teams2 and set nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} nfl_teams3.intersection_update(nfl_teams1, nfl_teams2) print(nfl_teams3)

Set isdisjoint() Method

If not one of the items is found in both sets, isdisjoint() returns True, otherwise it returns False.

Syntax

set.isdisjoint(set)

Parameter Values

ParameterDescription
setThis is essential. In this set, we are looking for items that are equal.

When set nfl_teams1 is not present in set nfl_teams2, return True:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Las Vegas Raiders", "Jacksonville Jaguars", "Houston Texans"} nfl_teams = nfl_teams1.isdisjoint(nfl_teams2) print(nfl_teams)

If set nfl_teams2 is not present in set nfl_teams1, return True:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Las Vegas Raiders", "Jacksonville Jaguars", "Houston Texans"} nfl_teams = nfl_teams2.isdisjoint(nfl_teams1) print(nfl_teams)

If neither set contains any items, what should you do? If both sets contain the same item, return False:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams = nfl_teams1.isdisjoint(nfl_teams2) print(nfl_teams)

What should you do if neither set contains any items? Provide False if the two sets include the identical item:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams = nfl_teams2.isdisjoint(nfl_teams1) print(nfl_teams)

Set issubset() Method

If all items in the set exist in the specified set, the issubset() method returns True, otherwise it returns False.

Syntax

set.issubset(set)

Parameter Values

Set

If all elements in set nfl_teams1 are available in set nfl_teams2, return True:

Example: 

nfl_teams1 = {"Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams = nfl_teams1.issubset(nfl_teams2) print(nfl_teams)

Return True if all elements in set nfl_teams2 are also available in set nfl_teams1:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears"} nfl_teams = nfl_teams2.issubset(nfl_teams1) print(nfl_teams)

If the specified set does not contain all items, what should be done?

If set nfl_teams2 does not contain all items in set nfl_teams1, return False:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams = nfl_teams1.issubset(nfl_teams2) print(nfl_teams)

What should be done if the given set does not include all items?

Return False if set nfl_teams1 does not include all elements in set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams = nfl_teams2.issubset(nfl_teams1) print(nfl_teams)

Set issuperset() Method

If all items in the specified set exist in the original set, then the issuperset() method returns True, otherwise it returns False.

Syntax

set.issuperset(set)

Parameter Values

Set

Delete the elements that are not present in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams1.intersection_update(nfl_teams2) print(nfl_teams1)

Delete the elements that are not present in both set nfl_teams1, set nfl_teams2 and set nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} nfl_teams1.intersection(nfl_teams2, nfl_teams3) print(nfl_teams1)

You can reverse the first example. Delete the elements that are not present in both set nfl_teams1, and set nfl_teams2:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams2.intersection_update(nfl_teams1) print(nfl_teams2)

You can reverse the second example. Delete the elements that are not present in both set nfl_teams1, set nfl_teams2 and set nfl_teams3:

Example: 

nfl_teams1 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams2 = {"Arizona Cardinals", "Atlanta Falcons", "Houston Texans"} nfl_teams3 = {"Kansas City Chiefs", "Atlanta Falcons", "Houston Texans"} nfl_teams3.intersection_update(nfl_teams1, nfl_teams2) print(nfl_teams3)

Set pop() Method

By calling pop() method, you can remove a random item from the set.

Returns the item that was removed using this method.

Syntax

set.pop()

From the nfl_teams set, eliminate a random item:

Example: 

nfl_teams = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams.pop() print(nfl_teams)

Take out a random item from the set nfl_teams1 and nfl_teams2:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams1.pop() nfl_teams2.pop() print(nfl_teams1) print(nfl_teams2)

Get the element that was removed:

Example: 

nfl_teams = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} mrx = nfl_teams.pop() print(mrx) #removed item

Delete the element from two sets and return it:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} mrx = nfl_teams1.pop() ample = nfl_teams2.pop() print(mrx) #nfl_teams1 removed item print(ample) #nfl_teams2 removed item

Set remove() Method

In the remove() method, the selected element is eliminated from the set.

The remove() method throws an error if the selected item does not exist, whereas the discard() method does not.

Syntax

set.remove(item)

Parameter Values

ParameterDescription
itemIt is necessary. The element to search for, and eliminate.

 

Take out “Chicago Bears” from the nfl_teams set:

Example: 

nfl_teams = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams.remove("Chicago Bears") print(nfl_teams)

Delete three items from the nfl_teams set:

Example: 

nfl_teams = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams.remove("Las Vegas Raiders") nfl_teams.remove("Los Angeles Rams") nfl_teams.remove("Jacksonville Jaguars") print(nfl_teams)

Set symmetric_difference() Method

symmetric_difference() method returns a set that includes all items from both sets, but not those that are present in both sets.

Result: Items that are not present in both sets appear in the returned set.

Syntax

set.symmetric_difference(set)

Parameter Values

Set

Create a set that includes all items from both sets, except those present in both sets:

Example: 

nfl_teams1 = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams2 = {"Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}mrx = nfl_teams1.symmetric_difference(nfl_teams2)print(mrx)

Provide a set that includes all items from both sets, except for those in both sets:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams2 = {"Chicago Bears", "Atlanta Falcons", "Dallas Cowboys","Kansas City Chiefs", "San Francisco 49ers", "Los Angeles Rams"}mrx = nfl_teams2.symmetric_difference(nfl_teams1)print(mrx)

Set symmetric_difference_update() Method

symmetric_difference_update() removes items from both sets, and inserts other items into the original set.

Syntax

set.symmetric_difference_update(set)

Parameter Values

Set

Insert the items that are not included in both sets, and eliminate the items that are present in both sets:

Example: 

nfl_teams1 = {"Las Vegas Raiders", "Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"}nfl_teams2 = {"Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}nfl_teams1.symmetric_difference_update(nfl_teams2)print(nfl_teams1)

Take out the items that are found in both sets, and insert the items that are absent:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Dallas Cowboys", "Houston Texans", "Chicago Bears", "Los Angeles Rams", "Atlanta Falcons"} nfl_teams2 = {"Chicago Bears", "Atlanta Falcons", "Dallas Cowboys","Kansas City Chiefs", "San Francisco 49ers", "Los Angeles Rams"}nfl_teams2.symmetric_difference_update(nfl_teams1)print(nfl_teams1)

Set union() Method

Union() returns a set containing all items from the original set and all items from the specified set(s).

By using commas, you can specify as many sets as you’d like.

Any iterable object can be used, not just sets.

The result will contain only one appearance of an item if it appears in more than one set.

Syntax

set.union(set1, set2...)

Parameter Values

ParameterDescription
set1It is necessary. To unify with the iterable
set2A choice. Unify with the other iterable.

It is possible to compare as many iterables as you like.

Use commas to separate each iteration.

Provide a set that includes all items from both sets, without duplicates:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams = nfl_teams1.union(nfl_teams2) print(nfl_teams)

All items from both sets should be included in a single set without any duplicates:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}nfl_teams = nfl_teams2.union(nfl_teams1) print(nfl_teams)

Now bringing together more than 3 sets:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams3 = {"Jacksonville Jaguars", "Las Vegas Raiders"} nfl_teams4 = {"Arizona Cardinals", "Chicago Bears", "Las Vegas Raiders"} nfl_teams = nfl_teams1.union(nfl_teams2, nfl_teams3, nfl_teams4) print(nfl_teams)

Combining more than three sets:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams3 = {"Jacksonville Jaguars", "Las Vegas Raiders"} nfl_teams4 = {"Arizona Cardinals", "Chicago Bears", "Las Vegas Raiders"} nfl_teams = nfl_teams4.union(nfl_teams2, nfl_teams3, nfl_teams1) print(nfl_teams)

Set update() Method

By calling the update() method, you can update the current set by adding items from another set (or any other iterable).

In the updated set, only one appearance of an item will appear if it appears in both sets.

Syntax

set.update(set)

Parameter Values

Set

Add the elements from set nfl_teams2 to set nfl_teams1:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams"}nfl_teams1.update(nfl_teams2)print(nfl_teams1)

Insert the elements from set nfl_teams2, nfl_teams3 and nfl_teams4 into set nfl_teams1:

Example: 

nfl_teams1 = {"Jacksonville Jaguars", "Las Vegas Raiders", "Atlanta Falcons", "Dallas Cowboys"} nfl_teams2 = {"Arizona Cardinals", "Chicago Bears", "Miami Dolphins", "Dallas Cowboys", "Los Angeles Rams"} nfl_teams3 = {"Jacksonville Jaguars", "Philadelphia Eagles"} nfl_teams4 = {"Pittsburgh Steelers", "Tennessee Titans", "Las Vegas Raiders"}nfl_teams1.update(nfl_teams2, nfl_teams3, nfl_teams4)print(nfl_teams1)

Python Set Methods Importance

Python set methods are important for several reasons:

  1. Sets ensure that each element is unique. By using set methods, you can easily eliminate duplicates and work with collections of distinct values. This is particularly useful when dealing with data that requires unique elements.
  2. Sets provide fast membership testing. The methods add(), remove(), discard(), and others allow you to efficiently check if an element is present in a set and perform operations accordingly.
  3. Set methods provide functionality for set operations such as union, intersection, difference, and symmetric difference. These operations enable you to combine, compare, and extract elements from sets based on set theory principles.
  4. Sets are useful for mathematical modeling and solving problems involving sets. The set methods facilitate operations such as finding subsets, checking for superset relationships, and performing set comparisons.
  5. Sets are effective for removing duplicate values from data collections. By converting a list or any other iterable to a set and back, you can easily eliminate duplicates and obtain a unique collection.
  6. Sets can be valuable in data analysis and data science workflows. They can be used to perform set-based operations on data, filter unique elements, identify common elements across multiple datasets, and more.
  7. Sets have a highly optimized implementation for handling operations such as adding, removing, and checking for membership. They are designed for efficient set-based operations, which can lead to improved performance in certain scenarios.
Get the latest technical information delivered to your inbox by subscribing to our Newsletter.
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 *