difference(): checking the difference between two Sets in Python

The difference() method checks the difference between two sets.

It returns a new set of the items contained in the first set that are not in the second set.

In the example below, both sets have ‘bmw’, but ‘mclaren’ and ‘ferrari’ are present only in the first set, so the output will be a new set containing these two items that exist only in the first set.

car_brands_set_one = {'bmw', 'mclaren', 'ferrari'}

car_brands_set_two = {'honda', 'bmw', 'ford'}

car_brands_set_difference = car_brands_set_one.difference(car_brands_set_two)

print(car_brands_set_difference)
{'mclaren', 'ferrari'}

To learn more about Sets, read this post Python Set: a quick reference