How to remove a set from set in python?

In this article we will discuss different ways to delete all elements of a set from another set in python.

Suppose we have two sets,

# First Set of Numbers
set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

# Second Set of Numbers
set_to_delete = {2, 11, 6}

Now we want to delete the elements of the second set, from the first set. Like in the above example we want to delete all items in the set set_to_delete to be deleted from the set set_of_num. After deletion contents of set set_of_num should be like,

{1, 4, 5, 7, 9}

There are different ways to do this. Let’s discuss them one by one,

Remove elements of a set from an another set using set.difference()

In Python, set class provides a member function difference(),

set.difference(iterable_sequences)

It accepts a single or multiple sequences and returns a new set containing the differences between them i.e. all elements which are in the calling set object but not in these sequences (passed as arguments). We can use that to remove the elements of a set from another set. For example,

# First Set of Numbers
set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

# Second Set of Numbers
set_to_delete = {2, 11, 6}

# Remove the contents of set_to_delete from set_of_num
set_of_num = set_of_num.difference(set_to_delete)

print('Modified contents of set: ')
print(set_of_num)

Output:

Modified contents of set: 
{1, 4, 5, 7, 9}

In the above example, we passed the set_to_delete as an argument to the difference() function. It returned the differences between set_of_num & set_to_delete. Basically elements which are present in set_of_num but not in set_to_delete. We assigned this returned set to the set_of_num. So, in other words we deleted the elements of one set from another set.

Using – operator to remove elements of a set from an another set

In previous example, instead of using difference() function, we can achieve the same result using – operator,

# First Set of Numbers
set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

# Second Set of Numbers
set_to_delete = {2, 11, 6}

# Remove the contents of set_to_delete from set_of_num
set_of_num = set_of_num - set_to_delete

print('Modified contents of set: ')
print(set_of_num)

Output:

Modified contents of set: 
{1, 4, 5, 7, 9}

Here we subtracted the set set_to_delete from set set_of_num. It gives the differences between set_of_num & set_to_delete, basically elements which are present in set_of_num but not in set_to_delete. Then we assigned this new set to the set_of_num. Which gives an effect that we deleted the elements of one set from another set.

Using difference_update() to remove elements of one set from an another set

Set difference() vs difference_update()

In both the previous examples, difference() function and – operator returns a new set, then we assign it back to the original set, to create an effect that the original set is modified. We can avoid this extra step if we use difference_update(). It works just like the difference() function, but instead of returning a new set, it modifies the calling set object.

So, let’s use this to remove elements of set set_to_delete from the set set_of_num i.e.

# First Set of Numbers
set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

# Second Set of Numbers
set_to_delete = {2, 11, 6}

# Remove elements of a set from another set & update the set in place
set_of_num.difference_update(set_to_delete)

print('Modified contents of set: ')
print(set_of_num)

Output:

Modified contents of set: 
{1, 4, 5, 7, 9}

Remove elements of one set from an another set using for loop

To remove elements of a set from another, we can also iterate over the elements of a set and for each element of that set, call discard() function on the other set object to delete that element from the set. For example,

# First Set of Numbers
set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

# Second Set of Numbers
set_to_delete = {2, 11, 6}

for elem in set_to_delete:
    set_of_num.discard(elem)

print('Modified contents of set: ')
print(set_of_num)

Output:

Modified contents of set: 
{1, 4, 5, 7, 9}

As discard() function does not give any error if the element is not present in the set. Therefore we called the discard() function on each element of set_to_delete, to get it deleted from set_of_num.

The complete example is as follows,

def main():

    print('Remove elements of one set from an another set using set.difference()')

    # First Set of Numbers
    set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

    # Second Set of Numbers
    set_to_delete = {2, 11, 6}

    # Remove the contents of set_to_delete from set_of_num
    set_of_num = set_of_num.difference(set_to_delete)

    print('Modified contents of set: ')
    print(set_of_num)

    print('Remove elements of one set from an another set using - operator')

    # First Set of Numbers
    set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

    # Second Set of Numbers
    set_to_delete = {2, 11, 6}

    # Remove the contents of set_to_delete from set_of_num
    set_of_num = set_of_num - set_to_delete

    print('Modified contents of set: ')
    print(set_of_num)

    print('Using difference_update() to remove elements of one set from an another set')

    # First Set of Numbers
    set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

    # Second Set of Numbers
    set_to_delete = {2, 11, 6}

    # Remove elements of a set from another set & update the set in place
    set_of_num.difference_update(set_to_delete)

    print('Modified contents of set: ')
    print(set_of_num)

    print('Remove elements of one set from an another set using for loop')

    # First Set of Numbers
    set_of_num = {1, 2, 11, 6, 7, 4, 5, 9}

    # Second Set of Numbers
    set_to_delete = {2, 11, 6}

    for elem in set_to_delete:
        set_of_num.discard(elem)

    print('Modified contents of set: ')
    print(set_of_num)


if __name__ == '__main__':
   main()

Output:

Remove elements of one set from an another set using set.difference()
Modified contents of set: 
{1, 4, 5, 7, 9}
Remove elements of one set from an another set using - operator
Modified contents of set: 
{1, 4, 5, 7, 9}
Using difference_update() to remove elements of one set from an another set
Modified contents of set: 
{1, 4, 5, 7, 9}
Remove elements of one set from an another set using for loop
Modified contents of set: 
{1, 4, 5, 7, 9}

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top