Home / Python / Reference / Set methods

Python set difference() method

Return the items in the first set but not the others.

Syntax

set.difference(*others) / a - b

Order matters: a − b is not b − a.

Parameters

*otherssets to subtract

Return value

a new set

Example

Code

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a - b, b - a)

Output

{1, 2} {5}

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.