Since sets are similar to dictionaries, they have a number of methods associated with them, which apply to both set and frozenset:
- len(s): This returns the number of items in set s
- x in s: This returns True if x exists in s; otherwise, it is False
- x not in s: This returns False if x exists in s; otherwise, it is True
- isdisjoint(other): This returns True if the set has no elements in common with object other
- issubset(other): This tests whether all elements in the set are also in other
- issuperset(other): This tests whether all elements in other are also in set
- union(*others): This returns a new set that includes elements from the original set and all other objects
- intersection(*others): This returns a new set that only contains objects that are in common between the set and all other objects
- difference(*others): This returns a new set that is only the elements that exist in the set, but are not in others
- symmetric_different(other): This returns a new set of elements that are either in set or other, but not both
- copy(): This returns a new set with a shallow copy of the set
The following are methods only available to set, but not to frozenset:
- update(*others): This updates the set by adding elements from all others
- intersection_update(*others): This updates the set by keeping only the elements that are in the set and others
- difference_update(*others): This updates the set by keeping only the elements found in others
- symmetric_difference_update(other): This updates the set with only the elements found in either set or other, but not common to both
- add(elem): This adds elem to the set
- remove(elem): This deletes elem from the set; it throws an exception if elem is not present
- discard(elem): This deletes elem from the set if present
- pop(): This removes elem from the set, if present, and returns its value; it throws an exception if the set contains no values
- clear(): This deletes all elements from the set