w3resource

Python Exercise: Create set difference

Python sets: Exercise-8 with Solution

Write a Python program to create set difference.

Sample Solution:-

Python Code:

setx = set(["apple", "mango"])
sety = set(["mango", "orange"])
setz = setx & sety
print(setz)
#Set difference
setb = setx - setz
print(setb)

Sample Output:

{'mango'}                                                                                                     
{'apple'} 

Pictorial Presentation:

NumPy Sets: Create set 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 a union of sets.
Next: Write a Python program to create a symmetric difference.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns True if there are duplicate values in a flat list, False otherwise

Example:

def tips_duplicates(lst):
  return len(lst) != len(set(lst))

x = [2, 4, 6, 8, 4, 2]
y = [1, 3, 5, 7, 9]
print(tips_duplicates(x))
print(tips_duplicates(y))

Output:

True
False