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.
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 outputtedprint('--')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/ Setsexample_set =set('hello')# set() turns an iterable into a setprint('example_set:', example_set)print('--')print('Number of Values:', len(example_set))# length functionprint('Minimum Value:', min(example_set))# min functionprint('Maximum Value:', max(example_set))# max functionprint('--')# tuple to settup = (2,3,5,7)print('tup to set:', set(tup))# list to setarray = ['orange']*2+ ['watermelon','apple'] + ['kiwi'] *10print('Original Array:', array)print('list to set:', set(array))
example_set: {'o', 'e', 'h', 'l'}
--
Number of Values: 4
Minimum Value: e
Maximum Value: o
--
tup to set: {2, 3, 5, 7}
Original Array: ['orange', 'orange', 'watermelon', 'apple', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi', 'kiwi']
list to set: {'watermelon', 'orange', 'apple', 'kiwi'}
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
Due to its unordered nature of a set, there is no concept of indexing or slicing with a set.
Set is however iterable.
# Iteration of a Setexample_set ={2,3,5,7,11,13}for v in example_set:print('Values of example_set:', v)
Values of example_set: 2
Values of example_set: 3
Values of example_set: 5
Values of example_set: 7
Values of example_set: 11
Values of example_set: 13
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
# Adding and Removing Valueslanguages =set()# empty set initializationprogramming_languages = ['C','C#','Java','Python','HTML','CSS','JavaScript','Haskell']for item in programming_languages: languages.add(item)# .add() method adds an item to a setprint('Languages set:', languages)print('--')languages.discard('HTML')# looks for the target value, if found, it will remove from the setprint('HTML deleted:', languages)languages.remove('CSS')# remove can be used to delete a value;# only difference is it will raise an error if the target is not foundprint('CSS deleted:', languages)random_remove = languages.pop()# .pop() method deletes a random value and return the value ... not recommendedprint('Randomly Removed value:', random_remove)languages.clear()# .clear() will empty out a set : output is set()print('Empty languages:', languages)
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.
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.
# Union Example:set1 =set('hello')set2 =set('world')result = set1 | set2 # | is the union operator ... all the members of both set are combined to a single set# Recall that: there are no duplicatesprint('set1 union set 2:', result)
set1 union set 2: {'h', 'w', 'e', 'r', 'l', 'd', 'o'}
Intersection: Members/Items that only exists in both sets.
# Union Example:set1 =set('hello')set2 =set('world')result = set1 & set2 # & is the intersection operatorprint('Intersection of set1 and set2:', result)
Intersection of set1 and set2: {'o', 'l'}
Difference: Members/items that only exists in the first set and not the second set.
# Difference Example:set1 =set('hello')set2 =set('world')result1 = set1 - set2 # - is the difference operator ... this is set1 difference set2result2 = set2 - set1 # set2 difference set1print('set1 - set2:', result1)print('set2 - set1:', result2)
A is proper subset of B if all members of A is found in B, but A cannot be exactly the same as B
# Proper Subset Example:set1 ={1,2,3}set2 ={1,2,3,4}set3 ={1,2,3}set4 =set('hello')print('Is set1 proper subset of set2?:', set1 < set2)# < is the proper subset operatorprint('Is set1 proper subset of set3?:', set1 < set3)print('Is set1 proper subset of set4?:', set1 < set4)
Is set1 proper subset of set2?: True
Is set1 proper subset of set3?: False
Is set1 proper subset of set4?: False
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
# Subset Example:set1 ={1,2,3}set2 ={1,2,3,4}set3 ={1,2,3}set4 =set('hello')print('Is set1 a subset of set2?:', set1 <= set2)# <= is the subset operatorprint('Is set1 a subset of set3?:', set1 <= set3)# Notice the difference in value hereprint('Is set1 a subset of set4?:', set1 <= set4)
Is set1 a subset of set2?: True
Is set1 a subset of set3?: True
Is set1 a subset of set4?: False
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 Example:set1 ={1,2,3,4}set2 ={1,2,3,4}set3 ={1,2,3}set4 =set('hello')print('Is set1 proper superset of set2?:', set1 > set2)# > is the proper superset operatorprint('Is set1 proper superset of set3?:', set1 > set3)print('Is set1 proper superset of set4?:', set1 > set4)
Is set1 proper superset of set2?: False
Is set1 proper superset of set3?: True
Is set1 proper superset of set4?: False
Superset: This is a boolean operator
A is a superset of B if A > B or A == B
# Superset Example:set1 ={1,2,3,4}set2 ={1,2,3,4}set3 ={1,2,3}set4 =set('hello')print('Is set1 superset of set2?:', set1 >= set2)# >= is the proper superset operatorprint('Is set1 superset of set3?:', set1 >= set3)print('Is set1 superset of set4?:', set1 >= set4)
Is set1 superset of set2?: True
Is set1 superset of set3?: True
Is set1 superset of set4?: False
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
# Disjoint Example# .isdisjoint() is a set method to check for such property between two sets.set1 ={1,2,3,4}set2 ={5,6,7}set3 ={1,2,3,4,5}print('set1 intersect set2:', set1 & set2)# Output is an empty setprint('set1 intersect set3:', set1 & set3)# Output is an non-empty setprint('--')print('set 1 disjoint set 2 check:', set1.isdisjoint(set2))# Therefore .isdisjoint() evaluates to Trueprint('set 1 disjoint set 3 check:', set2.isdisjoint(set3))
set1 intersect set2: set()
set1 intersect set3: {1, 2, 3, 4}
--
set 1 disjoint set 2 check: True
set 1 disjoint set 3 check: False
This is a way to affect an original set with another and assign the result back to the original set
# Union and Update --> Update the set, adding elements from all others.a =set('abracadabra')b =set('alacazam')a |= b # same as: a.update(b)print('Union Update:', a)# Intersection and Update --> Update the set, keeping only elements found in it and all others.a =set('abracadabra')b =set('alacazam')a &= b # same as: a.intersection_update(b)print('Intersection Update:', a)# Difference and Update --> Update the set, removing elements found in others.a =set('abracadabra')b =set('alacazam')a -= b # same as: a.difference_update(b)print('Difference Update:', a)# Symmetric Difference and Update --> Update the set, keeping only elements found in either set, but not in both.a =set('abracadabra')b =set('alacazam')a ^= b # same as: a.symmetric_difference_update(b)print('Symmeteric Difference Update:', a)
Much like list comprehension, sets support comprehension as well.
# Set Comprehension ExampledefisPalindrome(x):''' isPalindrome() returns True if string X is a palindrome '''return x == x[::-1]nums =list(range(1,10000))palindromic_set ={num for num in nums ifisPalindrome(str(num))}print('Palindromic Numbers Set from 1 to 10000:')print(palindromic_set)