w3resource

Python: Create set difference


Write a Python program to create set difference.

Visual Presentation:

NumPy Sets: Create set difference.

Sample Solution-1:

Using difference() Method

Python Code:

# Create a set 'setc1' with elements "green" and "blue":
setc1 = set(["green", "blue"])

# Create a set 'setc2' with elements "blue" and "yellow":
setc2 = set(["blue", "yellow"])

# Print a message to indicate the original sets:
print("Original sets:")

# Print the contents of 'setc1' and 'setc2':
print(setc1)
print(setc2)

# Calculate the difference of 'setc1' - 'setc2' and store it in 'r1':
r1 = setc1.difference(setc2)

# Print a message to indicate the difference of 'setc1 - setc2':
print("\nDifference of setc1 - setc2:")

# Print the result 'r1', which contains elements in 'setc1' that are not in 'setc2':
print(r1)

# Calculate the difference of 'setc2' - 'setc1' and store it in 'r2':
r2 = setc2.difference(setc1)

# Print a message to indicate the difference of 'setc2 - setc1':
print("\nDifference of setc2 - setc1:")

# Print the result 'r2', which contains elements in 'setc2' that are not in 'setc1':
print(r2)

# Create a set 'setn1' with repeated elements, including 1, 2, 3, 4, and 5:
setn1 = set([1, 1, 2, 3, 4, 5])

# Create a set 'setn2' with elements including 1, 5, 6, 7, 8, and 9:
setn2 = set([1, 5, 6, 7, 8, 9])

# Print a message to indicate the original sets:
print("\nOriginal sets:")

# Print the contents of 'setn1' and 'setn2':
print(setn1)
print(setn2)

# Calculate the difference of 'setn1' - 'setn2' and store it in 'r1':
r1 = setn1.difference(setn2)

# Print a message to indicate the difference of 'setn1 - setn2':
print("\nDifference of setn1 - setn2:")

# Print the result 'r1', which contains elements in 'setn1' that are not in 'setn2':
print(r1)

# Calculate the difference of 'setn2' - 'setn1' and store it in 'r2':
r2 = setn2.difference(setn1)

# Print a message to indicate the difference of 'setn2 - setn1':
print("\nDifference of setn2 - setn1:")

# Print the result 'r2', which contains elements in 'setn2' that are not in 'setn1':
print(r2)

# Create a set 'setc1' with elements "green" and "blue":
setc1 = set(["green", "blue"])

# Create a set 'setc2' with elements "blue" and "yellow":
setc2 = set(["blue", "yellow"])

# Print a message to indicate the original sets:
print("Original sets:")

# Print the contents of 'setc1' and 'setc2':
print(setc1)
print(setc2)

# Calculate the difference of 'setc1' - 'setc2' and store it in 'r1':
r1 = setc1 - setc2

# Print a message to indicate the difference of 'setc1 - setc2':
print("\nDifference of setc1 - setc2:")

# Print the result 'r1', which contains elements in 'setc1' that are not in 'setc2':
print(r1)

# Calculate the difference of 'setc2' - 'setc1' and store it in 'r2':
r2 = setc2 - setc1

# Print a message to indicate the difference of 'setc2 - setc1':
print("\nDifference of setc2 - setc1:")

# Print the result 'r2', which contains elements in 'setc2' that are not in 'setc1':
print(r2)

# Create a set 'setn1' with repeated elements, including 1, 2, 3, 4, and 5:
setn1 = set([1, 1, 2, 3, 4, 5])

# Create a set 'setn2' with elements including 1, 5, 6, 7, 8, and 9:
setn2 = set([1, 5, 6, 7, 8, 9])

# Print a message to indicate the original sets:
print("\nOriginal sets:")

# Print the contents of 'setn1' and 'setn2':
print(setn1)
print(setn2)

# Calculate the difference of 'setn1' - 'setn2' and store it in 'r1':
r1 = setn1 - setn2

# Print a message to indicate the difference of 'setn1 - setn2':
print("\nDifference of setn1 - setn2:")

# Print the result 'r1', which contains elements in 'setn1' that are not in 'setn2':
print(r1)

# Calculate the difference of 'setn2' - 'setn1' and store it in 'r2':
r2 = setn2 - setn1

# Print a message to indicate the difference of 'setn2 - setn1':
print("\nDifference of setn2 - setn1:")

# Print the result 'r2', which contains elements in 'setn2' that are not in 'setn1':
print(r2) 


Sample Output:

Original sets:
{'green', 'blue'}
{'yellow', 'blue'}

Difference of setc1 - setc2:
{'green'}

Difference of setc2 - setc1:
{'yellow'}

Original sets:
{1, 2, 3, 4, 5}
{1, 5, 6, 7, 8, 9}

Difference of setn1 - setn2:
{2, 3, 4}

Difference of setn2 - setn1:
{8, 9, 6, 7} 

Sample Solution-2:

Using - Operator

Python Code:

# Create a set 'setc1' with elements "green" and "blue":
setc1 = set(["green", "blue"])

# Create a set 'setc2' with elements "blue" and "yellow":
setc2 = set(["blue", "yellow"])

# Print a message to indicate the original sets:
print("Original sets:")

# Print the contents of 'setc1' and 'setc2':
print(setc1)
print(setc2)

# Calculate the difference of 'setc1' - 'setc2' and store it in 'r1':
r1 = setc1 - setc2

# Print a message to indicate the difference of 'setc1 - setc2':
print("\nDifference of setc1 - setc2:")

# Print the result 'r1', which contains elements in 'setc1' that are not in 'setc2':
print(r1)

# Calculate the difference of 'setc2' - 'setc1' and store it in 'r2':
r2 = setc2 - setc1

# Print a message to indicate the difference of 'setc2 - setc1':
print("\nDifference of setc2 - setc1:")

# Print the result 'r2', which contains elements in 'setc2' that are not in 'setc1':
print(r2)

# Create a set 'setn1' with repeated elements, including 1, 2, 3, 4, and 5:
setn1 = set([1, 1, 2, 3, 4, 5])

# Create a set 'setn2' with elements including 1, 5, 6, 7, 8, and 9:
setn2 = set([1, 5, 6, 7, 8, 9])

# Print a message to indicate the original sets:
print("\nOriginal sets:")

# Print the contents of 'setn1' and 'setn2':
print(setn1)
print(setn2)

# Calculate the difference of 'setn1' - 'setn2' and store it in 'r1':
r1 = setn1 - setn2

# Print a message to indicate the difference of 'setn1 - setn2':
print("\nDifference of setn1 - setn2:")

# Print the result 'r1', which contains elements in 'setn1' that are not in 'setn2':
print(r1)

# Calculate the difference of 'setn2' - 'setn1' and store it in 'r2':
r2 = setn2 - setn1

# Print a message to indicate the difference of 'setn2 - setn1':
print("\nDifference of setn2 - setn1:")

# Print the result 'r2', which contains elements in 'setn2' that are not in 'setn1':
print(r2)

Sample Output:

Original sets:
{'blue', 'green'}
{'yellow', 'blue'}

Difference of setc1 - setc2:
{'green'}

Difference of setc2 - setc1:
{'yellow'}

Original sets:
{1, 2, 3, 4, 5}
{1, 5, 6, 7, 8, 9}

Difference of setn1 - setn2:
{2, 3, 4}

Difference of setn2 - setn1:
{8, 9, 6, 7}

Sample Solution-3:

Multiple sets in the difference() method

Python Code:

# Create a set 'setn1' with repeated elements, including 1, 2, 3, 4, and 5:
setn1 = set([1, 1, 2, 3, 4, 5])

# Create a set 'setn2' with elements including 1, 5, 6, 7, 8, and 9:
setn2 = set([1, 5, 6, 7, 8, 9])

# Create a set 'setn3' with repeated elements, including 3, 4, 5, 9, and 10:
setn3 = set([3, 4, 5, 3, 9, 10])

# Create a set 'setn4' with elements including 5, 7, 9, 10, 12, and 14:
setn4 = set([5, 7, 9, 10, 12, 14])

# Print a message to indicate the original sets:
print("\nOriginal sets:")

# Print the contents of 'setn1', 'setn2', 'setn3', and 'setn4':
print(setn1)
print(setn2)
print(setn3)
print(setn4)

# Print a message to indicate the difference of 'setn1 - setn2, setn3, setn4':
print("\nDifference of setn1 - setn2, setn3, setn4:")

# Calculate the difference of 'setn1' - 'setn2', 'setn3', and 'setn4' and store it in 'r1':
r1 = setn1.difference(setn2, setn3, setn4)

# Print the result 'r1', which contains elements in 'setn1' that are not in any of the other sets:
print(r1)

# Print a message to indicate the difference of 'setn4 - setn1, setn2, setn3':
print("\nDifference of setn4 - setn1, setn2, setn3:")

# Calculate the difference of 'setn4' - 'setn1', 'setn2', and 'setn3' and store it in 'r2':
r2 = setn4.difference(setn1, setn2, setn3)

# Print the result 'r2', which contains elements in 'setn4' that are not in any of the other sets:
print(r2) 


Sample Output:

Original sets:
{1, 2, 3, 4, 5}
{1, 5, 6, 7, 8, 9}
{3, 4, 5, 9, 10}
{5, 7, 9, 10, 12, 14}

Difference of setn1 - setn2,setn3,setn4:
{2}

Difference of setn4 - setn1,setn2,setn3:
{12, 14}

Python Code Editor:

Previous: Write a Python program to create a union of sets.
Next: Write a Python program to create a symmetric difference.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.