Monday, May 23, 2022

Sets

Sets are unordered collections of hashable objects; in other words, each object is unique. Sets are commonly used to see if a collection of objects contains a particular item, remove duplicates from a sequence, and compute a variety of mathematical operations.

Sets look like dictionaries, in that curly braces {} are used to create a set. However, unlike dictionaries, sets only have values; there are no key names within a set.

The following example shows how to create a set:

knights_set = {"Sir Galahad", "Sir Lancelot", "Sir Robin"}

Sets are also like dictionaries in that the objects they contain are unordered, and it is likely that calling a set will show a different order of objects compared to what was originally set.

There are actually two types of sets: set and frozenset. A regular set is mutable, in that it can be modified in-place. A frozenset is immutable and cannot be altered after creation. Therefore, a frozenset can be used as a dictionary key, like a tuple, but a regular set cannot.

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:

there are some differences between set and frozenset methods:

  1. set1.isdisjoint(set2): Returns True if set1 has no elements in common with set2
  2. set1.issubset(set2): Returns True if every element in set1 exists in set2
  3. set1 < set2: Returns True if set1 is a true subset of set2 but not exactly equal to set2
  4. set1.issuperset(set2): Returns True if every element in set2 is in set1
  5. set1 > set2: Returns True if set1 is a true superset of set2 but not exactly equal to set2
  6. set1.union(set2, set3, ...): Returns a new set that includes elements from all given sets
  7. set1.intersection(set2, set3, ...): Returns a new set with all common elements between the given sets
  8. set1.difference(set2, set3, ...): Returns a new set with elements that exists in set1 but are not in any others
  9. set1.symmetric_difference(set2): Returns a new set with elements that are unique to each set
  10. set1.copy(): Returns a new set with a copy of the elements from set1

The following screenshot shows an example of set method:


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.

In the next post we will discuss some of the more common methods available to Python data types.

Share:

0 comments:

Post a Comment