# Python Set symmetric_difference_update()

Python’s `S.symmetric_difference_update(T)` determines all elements that are in exactly one of the two sets `S` or `T` and updates set `S` with those elements.

Here’s a minimal example where we update an existing set to consist of elements 1 and 4 that are in exactly one of the two sets `s` and `t`.

```>>> s = {1, 2, 3}
>>> t = {2, 3, 4}
>>> s.symmetric_difference_update(t)
>>> s
{1, 4}```

Here’s another visual example from Harry Potter: there are good wizards, bad wizards, and wizards that are both. The symmetric difference between those two sets consists of all wizards that are either good or bad, but not both—like Professor Snape.

```>>> good = {'ron', 'harry', 'hermione', 'dumbledore', 'snape'}
>>> bad = {'grindewald', 'voldemort', 'malfoy', 'snape'}
>>> good
{'ron', 'hermione', 'dumbledore', 'harry', 'grindewald', 'voldemort', 'malfoy'}```

## Syntax

Let’s dive into the formal syntax of the `set.symmetric_difference_update()` method.

`set.symmetric_difference_update(set)`

## Return Value

The return value of `set_1.symmetric_difference_update(set_2)` is `None`. But the side effect is that the original `set_1` now holds the elements that previously existed in exactly one of the sets `set_1` or `set_2`.

## Advanced Examples Set Symmetric Difference Update

There are some subtleties you need to understand regarding the set symmetric difference update function. Let’s dive into them by example!

The straightforward example is to calculate the symmetric difference of a set with another subset, and update the original set accordingly:

```>>> s = {1, 2, 3}
>>> s.symmetric_difference_update({1})
>>> s
{2, 3}```

But what if you’d invert this and calculate the symmetric difference between a subset and a superset? In this case, the result is the same set as before that contains the elements that are in exactly one of the two sets.

```>>> s = {1}
>>> s.symmetric_difference_update({1, 2, 3})
>>> s
{2, 3}```

Can you compute the symmetric difference between a set and an empty set? Sure! But it doesn’t change the original set:

```>>> s = {1, 2, 3}
>>> s.symmetric_difference_update(set())
>>> s
{1, 2, 3}```

## Set symmetric_difference() vs symmetric_difference_update()

The `set.symmetric_difference()` method returns a new set whereas the `set.symmetric_difference_update()` operates on the set it is called upon and returns `None`.

• `s.symmetric_difference(t)` – Return a new set with elements in either this set or the specified set argument, but not elements that are members of both.
• `s.symmetric_difference_update(t)` – Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both.

Here’s an example that shows the symmetric difference method:

```>>> s = {1, 2, 3}
>>> t = s.symmetric_difference({1, 2})
>>> s
{3}```

And the `set.symmetric_difference_update()` updates on an existing set `s` and doesn’t return anything:

```>>> s = {1, 2, 3}
>>> s.symmetric_difference_update({1, 2})
>>> s
{3}```

## What is the Time Complexity of set.symmetric_difference_update()?

The runtime complexity of the `set.symmetric_difference_update()` function on a set with n elements and a set argument with m elements is O(m+n) because you need to check for each element in both sets whether it is a member of the other set. Checking membership is O(1), so the runtime complexity is O(n) * O(1) + O(m) * O(1) = O(n+m).

You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:

I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:

```import matplotlib.pyplot as plt
import random
import time

sizes = [i * 10**5 for i in range(50)]
runtimes = []

for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))

# Start track time ...
t1 = time.time()
s.symmetric_difference(t)
t2 = time.time()
# ... end track time

runtimes.append(t2-t1)

plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')

plt.show()
```

## Other Python Set Methods

All set methods are called on a given set. For example, if you created a set `s = {1, 2, 3}`, you’d call `s.clear()` to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.