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 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
example_set = set('hello') # set() turns an iterable into a set
print('example_set:', example_set)
print('--')
print('Number of Values:', len(example_set)) # length function
print('Minimum Value:', min(example_set)) # min function
print('Maximum Value:', max(example_set)) # max function
print('--')
# tuple to set
tup = (2,3,5,7)
print('tup to set:', set(tup))
# list to set
array = ['orange']*2 + ['watermelon', 'apple'] + ['kiwi'] * 10
print('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
# Membership Example
example_set = set('hello')
print("Membership of: \'h\'", 'h' in example_set)
print("Non-Membership of: \'z\'", 'z' not in example_set)
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 Set
example_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 Values
languages = set() # empty set initialization
programming_languages = ['C', 'C#', 'Java', 'Python', 'HTML', 'CSS', 'JavaScript', 'Haskell']
for item in programming_languages:
languages.add(item) # .add() method adds an item to a set
print('Languages set:', languages)
print('--')
languages.discard('HTML') # looks for the target value, if found, it will remove from the set
print('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 found
print('CSS deleted:', languages)
random_remove = languages.pop() # .pop() method deletes a random value and return the value ... not recommended
print('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 duplicates
print('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 operator
print('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.
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 operator
print('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 operator
print('Is set1 a subset of set3?:', set1 <= set3) # Notice the difference in value here
print('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 operator
print('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 operator
print('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 set
print('set1 intersect set3:', set1 & set3) # Output is an non-empty set
print('--')
print('set 1 disjoint set 2 check:', set1.isdisjoint(set2)) # Therefore .isdisjoint() evaluates to True
print('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
Set Operators as Methods
# Union
a = set('abracadabra')
b = set('alacazam')
a.union(b)
print('Union:', a)
# Intersection
a = set('abracadabra')
b = set('alacazam')
a.intersection(b)
print('Intersection:', a)
# Difference
a = set('abracadabra')
b = set('alacazam')
a.difference(b)
print('Difference:', a)
# Symmeteric Difference
a = set('abracadabra')
b = set('alacazam')
a.symmetric_difference(b)
print('Symmetric Difference:', a)
# Subset
a = set('abracadabra')
b = set('alacazam')
print('Subset:', a.issubset(b))
# Superset
a = set('abracadabra')
b = set('alacazam')
print('Superset:', a.issuperset(b))
print('--')
# There are no proper subset/superset methods
# copy
a = set('abracadabra')
b = a.copy()
c = a
a.add('z')
print('.copy() examples:')
print('set a:', a)
print('set b:', b)
print('set c:', c)
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 Example
def isPalindrome(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 if isPalindrome(str(num))}
print('Palindromic Numbers Set from 1 to 10000:')
print(palindromic_set)