# Python Set difference_update() Method – Tutorial with Examples

The difference_update() method in Python Set is used to remove the elements from the set which are also present in the other specified set(s). This method updates the original set by removing the elements and returns None.

## Basic Syntax

```set.difference_update(set1, set2, ...)
```

Here, set is the set from which the elements have to be removed, and set1, set2, and so on are the sets which contain the elements to be removed.

### Return Value

The `difference_update()` method updates the original set by removing the elements that are also present in the other specified sets. It returns `None`.

## Examples

### Example 1: Removing elements from a set using difference_update()

In this example, we will remove the elements from a set that are present in another specified set using the difference_update() method.

```# Create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {2, 3}

# Remove the elements of set2 from set1
set1.difference_update(set2)

# Print the updated set
print(set1)
```

Output:

```{1, 4, 5}
```

In the above example, we first created two sets set1 and set2. We then used the difference_update() method to remove the elements of set2 from set1. The elements 2 and 3 are present in both set1 and set2. Therefore, after the operation, these elements are removed from set1.

### Example 2: Removing elements from a set using multiple sets

In this example, we will use multiple sets to remove the elements from a set using the difference_update() method.

```# Create three sets
set1 = {1, 2, 3, 4, 5}
set2 = {2, 3}
set3 = {3, 4, 5}

# Remove the elements of set2 and set3 from set1
set1.difference_update(set2, set3)

# Print the updated set
print(set1)
```

Output:

```{1}
```

In the above example, we created three sets set1, set2, and set3. We then used the difference_update() method to remove the elements of set2 and set3 from set1. The elements 2, 3, 4, and 5 are present in either set2 or set3 or both. Therefore, after the operation, these elements are removed from set1.

### Example 3: Removing all elements from a set using difference_update()

In this example, we will remove all elements from a set using the difference_update() method.

```# Create a set
set1 = {1, 2, 3, 4, 5}

# Remove all the elements from set1
set1.difference_update(set1)

# Print the updated set
print(set1)
```

Output:

```set()
```

In the above example, we created a set set1. We then used the difference_update() method to remove all the elements from set1. After the operation, the set set1 is empty.

## Use Cases

The difference_update() method can be used in a variety of scenarios, some of which are listed below:

• Removing common elements from two or more sets: The difference_update() method is useful when we have two or more sets and want to remove the common elements from one set.
• Modifying the original set: The difference_update() method modifies the original set, and this can be useful in scenarios where we want to keep the original set and remove some elements from it.
• Set operations: The difference_update() method can be used in conjunction with other set operations such as union(), intersection(), and symmetric_difference() to perform complex set operations efficiently.

In conclusion, the difference_update() method in Python Set is a powerful tool for removing elements from a set, and its ability to modify the original set makes it a useful tool in a variety of scenarios.