Sets in Python part 2

Sets in Python part 2

Set operations (Set theory related and augmented assignation)

The following operations over sets in Python work similarly to the respective operation in set theory.

Union

The union of a set with one or more, sets or iterable objects, returns a set that contains all items from the original set, and all the items from the specified sets or iterable objects.

If we only want the union of sets, and we don't plan to use an iterable object, we can use the | operator.

In case we plan to use iterable objects, we use the union.(<set/iterable>) method.

The update(<elem>) method, which we see in the adding section, is in fact the augmented assignment method of union, where we made the union and assigned the result to the set which is calling the method.

Following along, we can also use the augmented operator |= to unify two or more sets. As with the | operator, we can only unify sets and not iterable objects.

Code:

# Sets used
primary_colors = {"red","blue","green"}
another_colors_set = {"black","white"}

#List of colors
colors_list = ["red","pink","orange"]

# Sets union with the | ooperator
colors_union_operator = primary_colors | another_colors_set
print(f"\nThe new set created after performing union with the operator | has the following elements: {colors_union_operator}")

# Sets union with the union.(<set/iterable>) method with an iterable
colors_union_method = primary_colors.union(colors_list)
print(f"\nThe new set created after performing union with the .union(<set/iterable>) method has the following elements: {colors_union_method}")

# Sets union with the augmented operator |=
print(f"\nThe set third_colors_set has the following elements: {third_colors_set}")
third_colors_set |= primary_colors
print(f"\nThe set third_colors_set has the following elements after using the augmented operator |= : {third_colors_set}")

Output:

The set primary_colors has the following elements: {'blue', 'green', 'red'}

The set another_colors_set has the following elements: {'black', 'white'}

The new set created after performing union with the operator | has the following elements: {'green', 'black', 'blue', 'red', 'white'}

The new set created after performing union with the .union(<set/iterable>) method has the following elements: {'pink', 'blue', 'green', 'orange', 'red'}

The set third_colors_set has the following elements: {'purple', 'red', 'black'}

The set third_colors_set has the following elements after using the augmented operator |= : {'green', 'black', 'purple', 'blue', 'red'}

Intersection

The intersection of two or more sets returns a set with the elements common to all sets.

To do this operation, we can use the intersection(<set>) method or the & operator.

The augmented method and operator of intersection are: intersection_update(<set>) and &=, respectively.

The augmented version can be seen as a way to delete elements from a set, in which all the elements not common to the other sets are deleted.

Code:

# Sets intersection with the & operator
colors_intersection_operator = primary_colors & another_colors_set
print(f"\nThe new set with the intersection of the sets primary_colors and another_colors_set has the following elements: {colors_intersection_operator}")

# Sets intersections using the intersection(<set>) method
colors_intersection_method = another_colors_set.intersection(third_colors_set)
print(f"\nThe new set with the intersection of the sets another_colors_set, and the modified third_colors_set has the following elements: {colors_intersection_method}")

# Augmented assignation for intersection using the operator &=
# First we copy the set to avoid deletion of elements
primary_colors_copy = primary_colors.copy()
primary_colors_copy &= third_colors_set
print(f"\nThe set primary_colors_copy has the following elements after using the operator &= {primary_colors_copy}")

Output:

The new set with the intersection of the sets primary_colors and another_colors_set has the following elements: set()

The new set with the intersection of the sets another_colors_set, and the modified third_colors_set has the following elements: {'black'}

The set primary_colors_copy has the following elements after using the operator &= {'blue', 'green', 'red'}

Difference

The difference between two or more sets returns a set with all the elements only present in the first set, but not in the successive sets.

The method difference(<set>) and the operator - are used for this purpose.

The augmented method, difference_update(), and the augmented operator, -, can be used to delete the elements from the first set that exists in the other sets.

Code:

# Sets difference with the - operator
colors_difference_operator = primary_colors - another_colors_set
print(f"\nThe new set with the difference of the sets primary_colors and another_colors_set has the following elements: {colors_difference_operator}")

# Sets difference using difference(<set>) method 
colors_difference_method = primary_colors.difference(third_colors_set)
print(f"\nThe new set with the difference of the sets primary_colors, and the modified third_colors_set has the following elements: {colors_difference_method}")

# Augmented assignation for intersection using the operator -=
# First we copy the set to avoid deletion of elements
another_colors_set_copy = another_colors_set.copy()
another_colors_set_copy -= third_colors_set
print(f"\nThe set anothe_colors_set_copy has the following elements after using the operator -= {another_colors_set_copy}")

Output:

The new set with the difference of the sets primary_colors and another_colors_set has the following elements: {'blue', 'green', 'red'}

The new set with the difference of the sets primary_colors, and the modified third_colors_set has the following elements: set()

The set anothe_colors_set_copy has the following elements after using the operator -= {'white'}

Symmetric Difference

The symmetric difference between two sets returns a set that contains all the elements in either set, but not both. This operation can be understood and represented as the XOR logical operation.

The method of this operation, symmetric_diference(<set>), only admits one set as an argument, but the operator ^ allows applying the operation to multiple sets.

The augmented version updates the set by deleting the elements in the first set that also appear in the second set, and adding the elements from the second set that doesn't appear in the first set. The method, symmetric_diference_update(<set>), and operator, ^=, allows only one set.

Code:

#
# Symmetric Difference
#

# Symmetric difference with the ^ operator
colors_symmetric_difference_operator = primary_colors ^ another_colors_set
print(f"\nThe new set with the symmetric difference of the sets primary_colors and another_colors_set has the following elements: {colors_symmetric_difference_operator}")

# Symetric difference using symetric_difference(<set>) method
colors_symmetric_difference_method = primary_colors.symmetric_difference(third_colors_set)
print(f"\nThe new set with the symmetric difference of the sets primary_colors, and the another_colors_set has the following elements: {colors_symmetric_difference_method}")

# Augmented assignation for intersection using the operator ^=
# First we copy the set to avoid deletion of elements
another_colors_set_copy_2 = another_colors_set.copy()
another_colors_set_copy -= third_colors_set
print(f"\nThe set anothe_colors_set_copy_2 has the following elements after using the operator ^= {another_colors_set_copy_2}")

Output:

The new set with the symmetric difference of the sets primary_colors and another_colors_set has the following elements: {'black', 'blue', 'white', 'green', 'red'}

The new set with the symmetric difference of the sets primary_colors, and the another_colors_set has the following elements: {'black', 'purple'}

The set anothe_colors_set_copy_2 has the following elements after using the operator ^= {'black', 'white'}

Disjoint

This operation checks if two sets are disjoint or not, in other words, returns True if there aren't elements in common between the sets or False if at least one element is present in both sets.

This method, isdisjoint(<set>), admits one set or one iterable object.

Code:

print(f"\nAre another_colors_set and primary_colors disjointed?: {another_colors_set.isdisjoint(primary_colors)}")

Output:

Are another_colors_set and primary_colors disjointed?: True

Subsets and supersets

The issubset(<set>) method checks if the set calling the method is a subset of the set passed as an argument. A set, A, is said to be a subset of another set, B, if all the elements of A are present in set B. A set is a subset of itself.

The operator < checks if a set is a proper subset of another set. Contrary to a subset a proper subset doesn't consider a set to be a proper subset of itself or that the two sets are equal.

The issuperset(<set>) method checks if the set calling the method is a superset of the set passed as an argument. A set, A, is said to be a superset of another set, B, if all the elements of B are present in set A. A set is a superset of itself.

The operator > checks if a set is a proper superset of another set. A proper superset is the same as a superset, except that the sets can't be identical, so a set can't be a proper superset of itself.

Code:

# issubset(<set>) method
print(f"\nIs primary_colors a subset of third_colors_set?: {primary_colors.issubset(third_colors_set)}")

# Proper subset with the < operator
print(f"\nIs primary_colors a subset of third_colors_set?: {primary_colors < primary_colors}")

# issuperset(<set>) method
print(f"\nIs primary_colors a superset of another_colors_set?: {primary_colors.issuperset(another_colors_set)}")

# Proper subset with the < operator
print(f"\nIs primary_colors a superset of third_colors_set?: {primary_colors > third_colors_set}")

Output:

Is primary_colors a subset of third_colors_set?: True

Is primary_colors a subset of third_colors_set?: False

Is primary_colors a superset of another_colors_set?: False

Is primary_colors a superset of third_colors_set?: False

Feedback

This time I use code blocks instead of repls, I'd like to know what are your preferences. Do you prefer this style or repls?.

Nonetheless, below I leave the repl with all the code:

Further reading

Sets in python - Real Python

Python Sets - GeeksforGeeks

Python sets - W3Schools

Python - Set Methods - W3Schools

Mathematics | Set Operations (Set theory) - GeeksforGeeks