Set methods

Operations that are specific to sets and frozensets generally provide a way to quickly compare and shift out common/uncommon items between the different sets. Examples of the following methods can be seen in the next screenshot. The following non-exhaustive listing of set methods is an example of the more common set methods.

However, be sure to review the official Python documentation as there are some differences between set and frozenset methods:

The following screenshot shows an example of set method:

Set method examples

Lines 104 and 105 create two different sets. Lines 106-110 are self-explanatory, based on the previous definitions.

With line 111, we create a new set by merging set1 with set2. Line 112 shows a returned, empty set because there are no common elements between the two sets.

Line 113 shows the elements that exist in set1 but not set2, while line 114 shows all the unique elements.

Finally, line 115 presents a copy of set1; normally, this would be assigned to a new variable for later use.