{1, 2, 3, 4, 5}
.add(value)
adds an element to the set.discard(value)
discards the specified value.union(set2)
returns a set with the all elements in both sets.intersection(set2)
returns a set with the all elements in commonpresences = {"Anna", "Beatrice", "Claude"}
absences = {"Elvin", "Harley"}
absences.add("Becca")
presences.union(absences)
{'Anna', 'Beatrice', 'Becca', 'Claude', 'Elvin', 'Harley'}
common_values
False
if the sets have no elements in common, True
otherwisediscard_elements
set_1
and set_2
set_1
) removing all elements it has in common with set_2
def discard_elements(set_1, set_2):
to_remove = set_1.intersection(set_2)
for value in to_remove:
set_1.discard(value)
if __name__ == "__main__":
test_set = {1, 2, 3, 4}
discard_elements(test_set, {2})
assert test_set == {1, 3, 4}
print(test_set)
{1, 3, 4}