Python set functions and methods
Here’s a list of the functions and methods associated with sets in Python:
Set Methods
set.add(elem)
: Adds an element to the set.- Example:
my_set.add(4)
- Example:
set.update(*others)
: Updates the set by adding elements from all provided iterables.- Example:
my_set.update([5, 6], {7, 8})
- Example:
set.remove(elem)
: Removes the specified element from the set. Raises aKeyError
if the element is not found.- Example:
my_set.remove(4)
- Example:
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)
- Example:
set.pop()
: Removes and returns an arbitrary element from the set. Raises aKeyError
if the set is empty.- Example:
elem = my_set.pop()
- Example:
set.clear()
: Removes all elements from the set.- Example:
my_set.clear()
- Example:
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})
- Example:
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})
- Example:
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})
- Example:
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})
- Example:
set.issubset(other)
: ReturnsTrue
if the set is a subset ofother
.- Example:
is_subset = my_set.issubset({1, 2, 3, 4})
- Example:
set.issuperset(other)
: ReturnsTrue
if the set is a superset ofother
.- Example:
is_superset = my_set.issuperset({1, 2})
- Example:
set.isdisjoint(other)
: ReturnsTrue
if the set has no elements in common withother
.- Example:
is_disjoint = my_set.isdisjoint({5, 6})
- Example:
set.copy()
: Returns a shallow copy of the set.- Example:
new_set = my_set.copy()
- Example:
Common Built-in Functions with Sets
len(set)
: Returns the number of elements in the set.- Example:
size = len(my_set)
- Example:
sorted(set)
: Returns a sorted list of the set’s elements.- Example:
sorted_set = sorted(my_set)
- Example:
max(set)
: Returns the largest element in the set.- Example:
largest = max(my_set)
- Example:
min(set)
: Returns the smallest element in the set.- Example:
smallest = min(my_set)
- Example:
sum(set)
: Returns the sum of all elements in the set (only for sets with numeric values).- Example:
total = sum(my_set)
- Example:
any(set)
: ReturnsTrue
if any element in the set is truthy.- Example:
result = any(my_set)
- Example:
all(set)
: ReturnsTrue
if all elements in the set are truthy.- Example:
result = all(my_set)
- Example:
Summary of Set Methods and Functions
Method/Function | Description |
---|---|
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.