w3resource

Python Exercise: Create a symmetric difference

Python sets: Exercise-9 with Solution

Write a Python program to create a symmetric difference.

From Wikipedia, in mathematics, the symmetric difference, also known as the disjunctive union, of two sets is the set of elements which are in either of the sets and not in their intersection. The symmetric difference of the sets A and B is commonly denoted by

Python Sets: Create a symmetric difference.

For example, the symmetric difference of the sets {1,2,3} and {3,4} is {1,2,4}.

Python Sets: Create a symmetric difference.

Sample Solution:-

Python Code:

setx = set(["apple", "mango"])
sety = set(["mango", "orange"])
#Symmetric difference
setc = setx ^ sety
print(setc)

Sample Output:

{'apple', 'orange'}

Pictorial Presentation:

Python Sets: Create a symmetric difference.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create set difference.
Next: Write a Python program to check if a set is a subset of another set.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns all the elements of a list except the last one

Example:

def tips_initial(lst):
  return lst[0:-1]

print(tips_initial([1, 2, 3, 4]))

Output:

[1, 2, 3]