pyguides

Reference

Set Methods

Methods available on Python set objects.

  1. set.add()

    Adds an element to a set, creating a unique collection of hashable objects

  2. set.clear()

    Removes all elements from a set, leaving it empty

  3. set.copy()

    Creates a shallow copy of a set, returning a new set with the same elements

  4. set.difference_update()

    Removes all elements from this set that are also in another set, modifying the original set in place

  5. set.difference()

    Returns a new set containing elements in the calling set but not in the iterables

  6. set.discard()

    Removes an element from a set if it exists, without raising an error if the element is not found

  7. set.intersection()

    Returns a new set containing only elements that exist in all sets

  8. set.isdisjoint()

    Check if a set has no elements in common with another set

  9. set.issubset()

    Check whether all elements of one set are contained in another set

  10. set.issuperset()

    Check if a set contains all elements of another set

  11. set.pop()

    Removes and returns an arbitrary element from a set, raising KeyError if the set is empty

  12. set.remove()

    Removes an element from a set, raising KeyError if the element is not found

  13. set.symmetric_difference_update()

    Updates a set in-place with elements in either set but not in both

  14. set.symmetric_difference()

    Returns a new set containing elements in either set but not in both

  15. set.union()

    Returns a new set containing all elements from multiple sets

  16. set.update()

    Adds all elements from one or more iterables to a set, modifying it in place