set

A set object is an unordered collection of distinct hashable objects.

Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

There are currently two built-in set types, set and frozenset.

  • set([iterable]) - Return a set. The elements of a set must be hashable, bue the set type itself is mutable. The contents can be changed using methods like add() and remove(). Since set type is mutable, it has no hash value and cannot be used as either a dictionary key or an element of another set.
    1
    2
    3
    set((0, 1))  # set([0, 1])
    set('abc') # set(['a', 'b', 'c'])
    set(['abc']) # set(['abc'])

Non-empty sets(not frozensets) also can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'lizs'}, in addition to the set constructor.

  • frozenset([iterable]) - Return a frozenset. The elements of frozenset must be hashable and the frozenset type is immutable and hashable. its contents cannot be altered after it is created, it can therefore be used as a dictionary key or as an element of another set.
  • len(S) # set/frozenset

    Return the member of elements in set s (cardinality of s).

  • x in s # set/frozenset

  • x not in s # set/frozenset

  • isdisjoint(other) # set/frozenset

    Return True if the set has no elements in common with other.
    Sets are disjoint if and only if their intersection is the empty set.

  • issubset(other)

  • set <= other

    Test whether every element in set is in other.

  • set < other

    Test whether the set is a proper subset of other, that is set <= other and set != other.

  • issuperset(other)

  • set >= other

    Test whether every element in other is in the set.

  • set > other

    Test whether the set is a proper superset of other, that is, set >= other and set != other.

  • s.union(*other)

  • set | other

    Return a new set with elements from the set and all others.

  • s.intersection(*other)

  • set & other

    Return a new set with elements common to the set and all others.

  • s.difference(*others)

  • set - other

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

  • s.symmetric_difference(other)

  • set ^ other

    Return a new set with elements in either the set or other but not both.

  • s.copy()

    Return a new set with a shallow copy of s.

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

Instance of set are compared to instances of frozenset based on their members.
e.g. set('abc') == frozenset('abc') return True and so does set('abc') in set([frozenset('abc')])

Binary operations that mix set instances with frozenset return the type of first operand.
e.g. frozenset('ab') | set('bc') returns an instance of frozenset

The following table lists operations available for set that do not apply to immutable instances of frozenset:

  • s.update(*other)
  • set |= other

    Update the set, adding elements from all others.

  • s.intersection_update(*other)

  • set &= other

    Update the set, keeping only elements found in it and all others.

  • s.difference_update(*other)

  • set -= other

    Update the set, removing the elements found in others.

  • s.symmetric_difference_update(*other)

  • set ^= other

    Update the set, keeping only elements found in either set, but not in both.

  • s.add(elem) # set

    Add element elem to the set.

  • s.discard(elem)

    Remove element elem from the set if it is present.

  • s.remove(elem)

    Remove element elem from the set. Raises KeyError if elem is not contained in the set.

  • s.pop()

    Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.

  • s.clear()

    Remove all elements from the set.

Note, the non-operator versions of update(), intersection_update(),
difference_update(), symmetric_difference_update() methods will accept
any iterable as an argument.