set

Table of Contents

Overview

x in set
x not in set
set.isdisjoint(other)
set <= other           set.issubset(other)
set < other            set <= other and set != other
set >= other           set.issuperset(other)
set > other            set >= other and set != other
set | other | ...      set.union(*others)
set & other & ...      set.intersection(*others)
set - other - ...      set.difference(*others)
set ^ other            set.symmetric_difference(other)
                       (either the set or other but not both)
set.copy()             (a shallow copy of s)

# only for set, not frozenset
set |= other | ...     set.update(*others)
set &= other & ...     set.intersection_update(*others)
set -= other | ...     set.difference_update(*others)
set ^= other           set.symmetric_difference_update(other)
set.add(elem)
set.remove(elem)       (remove elem. KeyError if elem doesn't exist)
set.discard(elem)      (remove elem if it is present)
set.pop()              (remove and return an arbitrary elem. KeyError if empty)
clear()

non-operator versions of union(), intersection(), difference(), and symmetricdifference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets.

set('abc') == frozenset('abc') returns True and so does set('abc') in set([frozenset('abc')]).