Skip to content

Set

my_set = set()

Frozen Set

# Can be used as a dictionary key
> my_set = set([1,2,3,4])
> my_frozen_set = frozenset(my_set)
> my_other_frozenset = frozenset([3,3,2,4,1])
> my_other_frozenset == my_frozen_set
True

Length

len(s)

Membership

X in S     # Test X for membership in s
X not in S # Test X for non-membership in s

x.isdisjoint(s) # 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.

x.issubset(s) # Returns true if X is a subset of S otherwise false.

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

x.issuperset(s) # returns true if all elements of a set X occupies set S which is
                # passed as an argument and returns false if all elements of s 
                # are not present in x. ThiS means 
                # if X is a superset of S then it returns true; else False

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.

# Return a new set with elements from the set and all others.
union(*others)
set | other | ...

# Return a new set with elements common to the set and all others.
intersection(*others)
set & other & ...

# Return a new set with elements in the set that are not in the others.
difference(*others)
set - other - ...

# Return a new set with elements in either the set or other but not both.
symmetric_difference(other)
set ^ other
Note, the non-operator versions of union(), intersection(), difference(), symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like set('abc') & 'cbs' in favor of the more readable set('abc').intersection('cbs').

Update

# Update the set, adding elements from all others.
update(*others)
set |= other | ...

# Update the set, keeping only elements found in it and all others.
intersection_update(*others)
set &= other & ...

# Update the set, removing elements found in others.
difference_update(*others)
set -= other | ...

# Update the set, keeping only elements found in either set, but not in both.
symmetric_difference_update(other)
set ^= other

Add

my_set.add(elem)

Remove

my_set.remove(elem) # Raises KeyError if not present

Discard

my_set.discard(elem) # Remove is present

Pop

my_set.pop() # Remove and return an arbitrary element from the set. 
             # Raises KeyError if the set is empty.

Clear()

my_set.clear()

Length

len(my_set)

Example

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
> print(basket)                      # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
> 'orange' in basket                 # fast membership testing
True
> 'crabgrass' in basket
False

> # Demonstrate set operations on unique letters from two words
...
> a = set('abracadabra')
> b = set('alacazam')
> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
> a & b                              # letters in both a and b
{'a', 'c'}
> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}