Sets

  • A set is an unordered collection with no duplicate elements in Python 3.

  • Set is a mathematical way to describe collection of different unique objects.

  • By following the operations and characteristics of the mathematical set, we can utilize such data structure in our Python code.

  • The mathematical definition of the set can read in more details here.arrow-up-right

  • Sets can only contain immutable data type (Commonly Strings and Integers)

Using Sets in Python 3

How to Define a Set

# Set definition examples:
example_set1 = {1, 2, 3}
example_set2 = {'h','e','l','l','o'}

print('example_set1:', example_set1)
print('example_set2:', example_set2) # Notice there is only 1 'l'; Also notice the order of the values outputted
print('--')

singleton_set = {7}
empty_set = set() # this is because {} is reversed for a different feature in python 3.

print('Singleton:', singleton_set)
print('Empty Set:', empty_set)

Basic Built-in Functions w/ Sets

Basic Membership Operators

Membership is one of the key operations with set because:

  • A set has no duplicates

  • A setโ€™s membership operation is one of the fastest operations compared to strings, lists, or tuples this will be covered more when we look at the concept of: complexity

  • By using membership operator, we can be certain a target exists or does not exist in our data

Accessing Values in a Set

  • Due to its unordered nature of a set, there is no concept of indexing or slicing with a set.

  • Set is however iterable.

Python 3 Sets are mutable: Adding and Removing Values

  • Sets are mutable; therefore, we can add and remove values

  • There are also methods much lists that can affect the original sets as well

Powering Up Sets: Set Operators

Much like its mathematical counterpart, sets in Python 3 can utilize its vast operators to help us do complex calculations.

Most of these operators will have a method counterpart because sets are mutable.

circle-info

Note: We will be showcasing these operators with set of strings to make our lives easier, but this can be easily done with any kinds of sets.

Union: The joining/combining of two sets.

Intersection: Members/Items that only exists in both sets.

Difference: Members/items that only exists in the first set and not the second set.

Symmetric Difference: Members/items that exists one or the other set, but not both sets

Proper Subset: This is a boolean operator.

  • A is proper subset of B if all members of A is found in B, but A cannot be exactly the same as B

Subset: This is a boolean operator

  • A is a Proper Subset of B if A < B is True, but A can equal to B unlike a proper subset

Proper Superset: This is a boolean operator

  • A is a proper superset of B if A has all the values of B and more, but they are not equal to each other

Superset: This is a boolean operator

  • A is a superset of B if A > B or A == B

Disjoint: A Set Behaviour Property

Two set are consided disjointed when two sets share no common value.

  • Let A and B both represent a set.

  • If A & B is empty, then set A and B are considered disjointed.

  • To check this in python there is a method called: isdisjoint()

Recall: & is the intersection operator

Set Operators as Methods

Assignment Operation & Updating Methods

  • This is a way to affect an original set with another and assign the result back to the original set

Set Comprehension

Much like list comprehension, sets support comprehension as well.

Ending Notes on Sets for Python

  • Sets arenโ€™t sliceable nor indexable

  • Sets cannot have sets inside them

  • Sets do not have order; nor order of insertion

  • Sets cannot guarantee that their values will be in order

  • Sets do not record a valueโ€™s position

Last updated