Python: Find the elements in a given set that are not in another set
19. Find Elements in a Set Not in Another
Write a Python program to find elements in a given set that are not in another set.
Visual Presentation:

Sample Solution:
Python Code:
Sample Output:
Original sets: {1, 2, 3, 4, 5} {4, 5, 6, 7, 8} Difference of sn1 and sn2 using difference(): {1, 2, 3} Difference of sn2 and sn1 using difference(): {8, 6, 7} Difference of sn1 and sn2 using - operator: {1, 2, 3} Difference of sn2 and sn1 using - operator: {8, 6, 7}
For more Practice: Solve these Related Problems:
- Write a Python program to calculate the difference between two sets and return the elements unique to the first set.
- Write a Python program to use set difference to identify values present in one set but not in another.
- Write a Python program to implement a function that returns a new set of elements that exist in the first set but not in the second.
- Write a Python program to use the difference() method to compare two sets and output the unique elements.
Go to:
Previous: Write a Python program to check if a given set is superset of itself and superset of another given set.
Next: Write a Python program to remove the intersection of a 2nd set from the 1st set.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.