Sets Flashcards
Create set
set_variable = set()
Add value to set
set_variable.add(value)
Check if value exists in set
value in set_variable
Delete value from set
set_variable.remove(value)
Remove duplicates from list
unique_list = list(set(original_list))
Find length of set
len(set_variable)
Clear set
set_variable.clear()
Union set, returns new set which includes all elements from set1 and set2
union = set1.union(set2)
Find the intersection of two sets
intersection = set1.intersection(set2)
Find the elements that are in set1 but not set2
set1.difference(set2)
Find the elements that aren’t shared by both sets
symmetric_difference = set1.symmetric_difference(set2)
Find is set1 is a subset of superset of set2
set1.issubset(set2) # elements of set1 are all present in set2
set1.issuperset(set2) # elements of set2 are all present in set1
Create immutable set
frozen_set = frozenset([1, 2, 3])
Iterate over values in set
for value in set_variable:
print(value)