Python set minus | Example code

Using the minus set to get the difference between the two sets. Using the difference method there is no difference in the output.

difference_of_sets = set_1 - set_2

Python set minus example

Simple example code subtraction in sets python

set1 = {1, 2, 3, 4, 5}

set2 = {1, 2, 3}

print(set1 - set2)

Output:

Python set minus

The syntax for other set functions:

set_1 | set_2 # Union of sets (elements in both)
set_1 & set_2 # Intersection of sets (elements in common)

Do comment if you have any doubts and suggestions on this Python set topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.