Skip to content
Set Methods

difference()

Return a new set with elements in this set that are not in others.

set.difference(*others)

set.difference() returns a new set containing all elements present in the calling set but absent from all provided others iterables.

You can also use the subtraction operator (set_a - set_b).

Parameters

NameDescription
*othersOne or more sets or iterables whose elements are excluded from the result.

Returns

A new set containing the difference.

Try it

Run it and change it
all_students = {"Alice", "Bob", "Charlie", "David"}
submitted = {"Alice", "David"}
missing = all_students.difference(submitted)
print(sorted(missing))

Worth knowing

  • Operator vs Method: The - operator requires both operands to be sets, whereas the difference() method accepts any iterable (lists, tuples, strings).

Related entries