Python set functions and methods


Here’s a list of the functions and methods associated with sets in Python:

Set Methods

  1. set.add(elem): Adds an element to the set.

    • Example: my_set.add(4)
  2. set.update(*others): Updates the set by adding elements from all provided iterables.

    • Example: my_set.update([5, 6], {7, 8})
  3. set.remove(elem): Removes the specified element from the set. Raises a KeyError if the element is not found.

    • Example: my_set.remove(4)
  4. set.discard(elem): Removes the specified element from the set if it is present. Does nothing if the element is not found.

    • Example: my_set.discard(4)
  5. set.pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.

    • Example: elem = my_set.pop()
  6. set.clear(): Removes all elements from the set.

    • Example: my_set.clear()
  7. set.union(*others): Returns a new set with elements from the set and all provided sets (union operation).

    • Example: new_set = my_set.union({5, 6})
  8. set.intersection(*others): Returns a new set with elements common to the set and all provided sets (intersection operation).

    • Example: new_set = my_set.intersection({1, 2, 3})
  9. set.difference(*others): Returns a new set with elements in the set that are not in any of the provided sets (difference operation).

    • Example: new_set = my_set.difference({2, 3})
  10. set.symmetric_difference(other): Returns a new set with elements in either the set or the other set, but not in both (symmetric difference operation).

    • Example: new_set = my_set.symmetric_difference({3, 4})
  11. set.issubset(other): Returns True if the set is a subset of other.

    • Example: is_subset = my_set.issubset({1, 2, 3, 4})
  12. set.issuperset(other): Returns True if the set is a superset of other.

    • Example: is_superset = my_set.issuperset({1, 2})
  13. set.isdisjoint(other): Returns True if the set has no elements in common with other.

    • Example: is_disjoint = my_set.isdisjoint({5, 6})
  14. set.copy(): Returns a shallow copy of the set.

    • Example: new_set = my_set.copy()

Common Built-in Functions with Sets

  1. len(set): Returns the number of elements in the set.

    • Example: size = len(my_set)
  2. sorted(set): Returns a sorted list of the set’s elements.

    • Example: sorted_set = sorted(my_set)
  3. max(set): Returns the largest element in the set.

    • Example: largest = max(my_set)
  4. min(set): Returns the smallest element in the set.

    • Example: smallest = min(my_set)
  5. sum(set): Returns the sum of all elements in the set (only for sets with numeric values).

    • Example: total = sum(my_set)
  6. any(set): Returns True if any element in the set is truthy.

    • Example: result = any(my_set)
  7. all(set): Returns True if all elements in the set are truthy.

    • Example: result = all(my_set)

Summary of Set Methods and Functions

Method/FunctionDescription
set.add(elem)Adds an element to the set.
set.update(*others)Adds elements from others to the set.
set.remove(elem)Removes an element, raises error if not found.
set.discard(elem)Removes an element, does nothing if not found.
set.pop()Removes and returns an arbitrary element.
set.clear()Removes all elements from the set.
set.union(*others)Returns the union of the set and others.
set.intersection(*others)Returns the intersection of the set and others.
set.difference(*others)Returns the difference between the set and others.
set.symmetric_difference(other)Returns the symmetric difference of the set and another.
set.issubset(other)Checks if the set is a subset of another.
set.issuperset(other)Checks if the set is a superset of another.
set.isdisjoint(other)Checks if the set and another have no elements in common.
set.copy()Returns a copy of the set.
len(set)Returns the size of the set.
sorted(set)Returns a sorted list of the set elements.
max(set)Returns the largest element in the set.
min(set)Returns the smallest element in the set.
sum(set)Returns the sum of all numeric elements in the set.
any(set)Returns True if any element is truthy.
all(set)Returns True if all elements are truthy.

These methods and functions provide the primary tools to manipulate, compare, and retrieve information from sets in Python.