## Introduction

The **difference** of sets is an operation that **removes** all elements from one set that are **also in another**. In Python, **set intersection** can be accomplished with the `difference()`

instance method, the `difference()`

class method, and the set difference operator, `-`

, i.e. the minus sign character. Each of these options provides a slightly different capability and convenience. They work in a similar way to the options provided for the set union and intersection, and familiarity with those makes learning about set difference straightforward.

## Using the *difference()* Instance Method

The `difference()`

instance method of a set object extracts elements that are present in the set but not in one or more other iterables. This creates a new set containing only those values that are unique to the original set.

The example below, takes the difference of two sets.

**Example:**

set1 = {1, 2, 3} set2 = {1, 4, 5} difference_set = set1.difference(set2) print(difference_set)

**Output:**

{2, 3}

In this example, the `difference()`

method is used to find elements unique to `set1`

when compared to `set2`

. Since `set2`

contains `1`

, which is also in `set1`

, it is eliminated, resulting in the set `{2, 3}`

.

The next example shows a more complicated set difference operation.

**Example:**

set1 = {1, 2, 3} lst = [1, 4, 5] set2 = {2, 6, 7} difference_set = set1.difference(lst, set2) print(difference_set)

**Output:**

{3}

In this example, the `difference()`

method is used to find elements unique to `set1`

when compared to the list, `lst`

, and another set, `set2`

. `lst`

contains `1`

, and `set2`

contains `2`

, which are both removed, resulting in a new set that contains the only remaining element in the original set, `{3}`

.

## Using the Python Set Difference Operator `-`

The set difference operator, `-`

, is an alternative way to creating a set difference. Unlike the `difference()`

method, it can only be used with proper set objects and not any other iterable type. Using `-`

with iterable objects that are not sets results in an error.

**Example:**

set1 = {1, 2, 3} set2 = {1, 4, 5} set3 = {2, 6, 7} difference_set = set1 - set2 - set3 print(difference_set)

**Output:**

{3}

In this example, the difference of `set1`

and the two sets `set2`

and `set3`

is generated with `-`

operators.

## Using the *difference()* Class Method

The `difference()`

class method, similar to the `difference()`

instance method, allows you to find the difference between a set and other iterable objects.

It is a class method of the `set`

class, and you call it with a `set.`

prefix.

The difference between the `difference()`

instance method and `difference()`

class method is that the instance method is called on a specific set, whereas the class method includes the set as its first parameter. Following the first argument, which must be a set, the rest of the arguments can be objects of any iterable type.

**Example:**

set1 = {1, 2, 3} set2 = {1, 4, 5} lst1 = [2, 6, 7] difference_set = set.difference(set1, set2, lst1) print(difference_set)

**Output:**

{3}

After the difference operation, `difference_set`

in this example contains elements unique to `set1`

which are not in `set2`

or `lst1`

.

## Set Difference with Mutable Sets

Similar to set intersection and union, set difference operations do not modify the original set. Instead, they create a new set containing the difference. If you want to modify the original set variable to contain the difference result, you should do it explicitly by assigning the result back to it.

**Example:**

my_set = {1, 2, 3} another_set = {3, 4, 5} my_set = my_set.difference(another_set) print(my_set)

**Output:**

{1, 2}

After this operation, the original set, `my_set`

, contains the resulting difference, `{1, 2}`

.

## Summary & Reference for Python Set Difference

Set difference is a fundamental set operation in Python for extracting elements that are unique to one set compared to others.

The `difference()`

instance method extracts elements unique to a set object when compared to other iterable objects, creating a new set.

set1 = {1, 2, 3} lst1 = [1, 4, 5] set2 = {2, 5, 7} difference_set = set1.difference(set2, lst1) # difference_set --> {3}

The set difference operator, `-`

, allows you to find elements unique to one set as compared to other proper sets. It creates a new set containing only the unique values.

set1 = {1, 2, 3} set2 = {1, 4, 5} set3 = {2, 6, 7} difference_set = set1 - set2 - set3 # difference_set --> {3}

The `difference()`

class method of the `set`

class lets you find elements unique to a `set`

object, given as the first argument, compared to other iterable objects, given as subsequent arguments.

set1 = {1, 2, 3} set2 = {1, 2, 4} lst1 = [1, 2, 5] difference_set = set.difference(set1, set2, lst1) # difference_set --> {3}

Set difference operations don’t modify the original sets but create new sets. If you want to modify the original set variable with the difference result, you should assign the result back explicitly.

my_set = {1, 2, 3} another_set = {3, 4, 5} my_set = my_set.difference(another_set) # my_set --> {1, 2}