Skip to content

set

Set literals {1, 2}, set(iterable), and set algebra operators. Members must be hashable (tuples of hashables and properly hashable user-class instances included).

MethodReturnsMeaning
.lengthnumberUnique members, same as len(s). Property: no parens
.add(item) / .remove(item) / .discard(item)NoneAdd; remove (raises if absent); remove safely
.pop()anyRemove and return an arbitrary element (deterministic insertion order, but don’t rely on it); raises when empty
.has(item)booleanMembership, equivalent to item in s
.clear() / .copy()None / setEmpty the set; shallow copy
.union(*others) / .intersection(*others) / .difference(*others)setNew set from set algebra over finite iterables (no arguments = copy)
.symmetric_difference(other)setMembers in exactly one side, same as a ^ b
.update(*others) / .intersection_update(*others) / .difference_update(*others) / .symmetric_difference_update(other)NoneIn-place versions
.issubset(other) / .issuperset(other) / .isdisjoint(other)booleana <= b / a >= b / no shared members