Python Set symmetric_difference_update() Method with Examples

Set symmetric_difference_update() Method:

The symmetric_difference_update() method updates the original set by removing items from both sets and inserting new items.

The set of elements that are in either P or Q but not in their intersection is the symmetric difference of two sets P and Q.

For Example:

If P and Q are two distinct sets. The symmetric_difference_update of the given two sets is :

Let P={4, 5, 6, 7}

Q={5, 6, 8, 9}

Here {5, 6} are the common elements in both sets.

so, Set P is updated with all the other items from both sets except the common elements.

Set Q, on the other hand, remains unchanged.

Output : P={4, 7, 8, 9} and Q={5, 6, 8, 9}

Syntax:

set.symmetric_difference_update(set)

Parameters

set: This is Required. The set to look for matches in.

Return Value:

None is returned by symmetric difference update() (returns nothing). Rather, it calls the set and updates it.

Examples:

Example1:

Input:

Given first set = {10, 20, 50, 60, 30}
Given second set = {20, 30, 80, 90}

Output:

The given first set =  {10, 80, 50, 90, 60}
The given second set =  {80, 90, 20, 30}
The result after applying symmetric_difference_update() method =  None

Example2:

Input:

Given first set = {'a', 'b', 'c'}
Given second set = {'a', 'b', 'c', 'd', 'e'}

Output:

The given first set =  {'d', 'e'}
The given second set =  {'a', 'e', 'b', 'd', 'c'}
The result after applying symmetric_difference_update() method =  None

Set symmetric_difference_update() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Apply symmetric_difference_update() method to the given two sets and Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the result after applying the symmetric_difference_update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 50, 60, 30}
# Give the second set as static input and store it in another variable.
scnd_set = {20, 30, 80, 90}
# Apply symmetric_difference_update() method to the given two sets and
# Store it in another variable.
rslt = fst_set.symmetric_difference_update(scnd_set)
# Print the given first set
print("The given first set = ", fst_set)
# Print the given second set
print("The given second set = ", scnd_set)
# Print the result after applying symmetric_difference_update() method for the
# given two sets
print("The result after applying symmetric_difference_update() method = ", rslt)

Output:

The given first set =  {10, 80, 50, 90, 60}
The given second set =  {80, 90, 20, 30}
The result after applying symmetric_difference_update() method =  None

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply symmetric_difference_update() method to the given two sets and Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the result after applying the symmetric_difference_update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))

# Apply symmetric_difference_update() method to the given two sets and
# Store it in another variable.
rslt = fst_set.symmetric_difference_update(scnd_set)
# Print the given first set
print("The given first set = ", fst_set)
# Print the given second set
print("The given second set = ", scnd_set)
# Print the result after applying symmetric_difference_update() method for the
# given two sets
print("The result after applying symmetric_difference_update() method = ", rslt)

Output:

Enter some random Set Elements separated by spaces = 2 5 6 7 1
Enter some random Set Elements separated by spaces = 6 9 1 4 0
The given first set = {0, 2, 4, 5, 7, 9}
The given second set = {0, 1, 4, 6, 9}
The result after applying symmetric_difference_update() method = None