Set operations (e.g., determining union sets, product sets, and subsets) with Python’s set type

Money and Business

Python provides a built-in data type, set, which handles sets.

The type set is a collection of non-duplicate elements (elements that are not the same value, unique elements) and can perform set operations such as union set, product set, and difference set.

In this section, basic operations in set-type set operations are explained with sample code.

  • Creation of set objects: {},set()
  • set inclusion notation
  • Number of elements in the set:len()
  • Adding an Element to a Set:add()
  • Remove an element from a set: discard(),remove(),pop(),clear()
  • Wasset (merger, union):|operator,union()
  • Product sets (common parts, intersections, intersections):& operator,intersection()
  • relative complement:-operator,difference()
  • symmetry difference set:^ operator,symmetric_difference()
  • subset or not:<= operator,issubset()
  • Upper set or not:>= operator,issuperset()
  • Determination of whether they are mutually prime or not:isdisjoint()

The set type is a mutable type that can add and delete elements, and there is also a frozenset type that has the same set operation and other methods as the set type but is immutable (cannot be modified by adding, deleting, or otherwise modifying elements).

Creation of set object:: {}, set()

Generated by wave brackets {}

Objects of type set can be created by enclosing elements in braces {}.

If there are duplicate values, they are ignored and only unique values remain as elements.

s = {1, 2, 2, 3, 1, 4}

print(s)
print(type(s))
# {1, 2, 3, 4}
# <class 'set'>

It is possible to have different types as elements. However, updatable objects such as list types cannot be registered. Tuples are allowed.

Also, since set types are unordered, the order in which they are generated is not stored.

s = {1.23, 'abc', (0, 1, 2), 'abc'}

print(s)
# {(0, 1, 2), 1.23, 'abc'}

# s = {[0, 1, 2]}
# TypeError: unhashable type: 'list'

Different types, such as int and float, are considered duplicates if their values are equivalent.

s = {100, 100.0}

print(s)
# {100}

Since an empty brace {} is considered a dictionary type, an empty set type object (empty set) can be created using the constructor described next.

s = {}

print(s)
print(type(s))
# {}
# <class 'dict'>

Generated by constructor set()

Objects of type set can also be created with the constructor set().

Specifying an iterable object such as a list or tuple as an argument generates a set object whose elements are unique values only, with duplicate elements excluded.

l = [1, 2, 2, 3, 1, 4]

print(l)
print(type(l))
# [1, 2, 2, 3, 1, 4]
# <class 'list'>

s_l = set(l)

print(s_l)
print(type(s_l))
# {1, 2, 3, 4}
# <class 'set'>

Immutable frozenset types are created with the constructor frozenset().

fs_l = frozenset(l)

print(fs_l)
print(type(fs_l))
# frozenset({1, 2, 3, 4})
# <class 'frozenset'>

If the argument is omitted, an empty set-type object (empty set) is created.

s = set()

print(s)
print(type(s))
# set()
# <class 'set'>

Duplicate elements can be removed from a list or tuple using set(), but the order of the original list is not preserved.

To convert a set type into a list or tuple, use list(),tuple().

l = [2, 2, 3, 1, 3, 4]

l_unique = list(set(l))
print(l_unique)
# [1, 2, 3, 4]

See the following article for information on removing duplicate elements while preserving order, extracting only duplicate elements, and processing duplicate elements in a two-dimensional array (list of lists).

set inclusion notation

As well as list comprehensions, there are set comprehensions. Simply replace the square brackets [] with braces {} in list comprehensions.

s = {i**2 for i in range(5)}

print(s)
# {0, 1, 4, 9, 16}

See the following article for more information on list comprehension notation.

Number of elements in the set: len()

The number of elements in a set can be obtained with the built-in function len().

s = {1, 2, 2, 3, 1, 4}

print(s)
print(len(s))
# {1, 2, 3, 4}
# 4

If you want to count the number of elements in each list that has elements with duplicate values, etc., see the following article.

Adding an Element to a Set: add()

To add an element to a set, use the add() method.

s = {0, 1, 2}

s.add(3)
print(s)
# {0, 1, 2, 3}

Remove an element from a set: discard(),remove(),pop(),clear()

To remove an element from a set, use the discard(), remove(), pop(), and clear() methods.

The discard() method deletes the element specified in the argument. If a value that does not exist in the set is specified, nothing is done.

s = {0, 1, 2}

s.discard(1)
print(s)
# {0, 2}

s = {0, 1, 2}

s.discard(10)
print(s)
# {0, 1, 2}

The remove() method also removes the element specified in the argument, but an error KeyError is returned if a value that does not exist in the set is specified.

s = {0, 1, 2}

s.remove(1)
print(s)
# {0, 2}

# s = {0, 1, 2}

# s.remove(10)
# KeyError: 10

The pop() method removes elements from a set and returns their values. It is not possible to select which values to remove. An empty set will result in a KeyError error.

s = {2, 1, 0}

v = s.pop()

print(s)
print(v)
# {1, 2}
# 0

s = {2, 1, 0}

print(s.pop())
# 0

print(s.pop())
# 1

print(s.pop())
# 2

# print(s.pop())
# KeyError: 'pop from an empty set'

The clear() method removes all elements and makes the set empty.

s = {0, 1, 2}

s.clear()
print(s)
# set()

Wasset (merger, union): |operator, union()

The union set (merger, union) can be obtained with the | operator or the union() method.

s1 = {0, 1, 2}
s2 = {1, 2, 3}
s3 = {2, 3, 4}

s_union = s1 | s2
print(s_union)
# {0, 1, 2, 3}

s_union = s1.union(s2)
print(s_union)
# {0, 1, 2, 3}

Multiple arguments can be specified for a method. In addition to the set type, lists and tuples that can be converted to the set type by set() can also be specified as arguments. The same applies to subsequent operators and methods.

s_union = s1.union(s2, s3)
print(s_union)
# {0, 1, 2, 3, 4}

s_union = s1.union(s2, [5, 6, 5, 7, 5])
print(s_union)
# {0, 1, 2, 3, 5, 6, 7}

Product sets (common parts, intersections, intersections): & operator, intersection()

The product set (common part, intersection, and intersection) can be obtained with the & operator or the intersection() method.

s_intersection = s1 & s2
print(s_intersection)
# {1, 2}

s_intersection = s1.intersection(s2)
print(s_intersection)
# {1, 2}

s_intersection = s1.intersection(s2, s3)
print(s_intersection)
# {2}

relative complement: -operator, difference()

The difference set can be obtained with the – operator or the difference() method.

s_difference = s1 - s2
print(s_difference)
# {0}

s_difference = s1.difference(s2)
print(s_difference)
# {0}

s_difference = s1.difference(s2, s3)
print(s_difference)
# {0}

symmetry difference set: ^ operator, symmetric_difference()

The symmetric difference set (the set of elements contained in only one of the two) can be obtained with the ^ operator or symmetric_difference().

Equivalent to exclusive disjunction (XOR) in logical operations.

s_symmetric_difference = s1 ^ s2
print(s_symmetric_difference)
# {0, 3}

s_symmetric_difference = s1.symmetric_difference(s2)
print(s_symmetric_difference)
# {0, 3}

subset or not: <= operator, issubset()

To determine whether a set is a subset of another set, use the <= operator or the issubset() method.

s1 = {0, 1}
s2 = {0, 1, 2, 3}

print(s1 <= s2)
# True

print(s1.issubset(s2))
# True

Both the <= operator and the issubset() method return true for equivalent sets.

To determine if it is a true subset, use the <= operator, which returns false for equivalent sets.

print(s1 <= s1)
# True

print(s1.issubset(s1))
# True

print(s1 < s1)
# False

Upper set or not: >= operator, issuperset()

To determine if one set is a superset of another, use the >= operator or issuperset().

s1 = {0, 1}
s2 = {0, 1, 2, 3}

print(s2 >= s1)
# True

print(s2.issuperset(s1))
# True

Both the >= operator and the issuperset() method return true for equivalent sets.

To determine if it is a true superset, use the >= operator, which returns false for equivalent sets.

print(s1 >= s1)
# True

print(s1.issuperset(s1))
# True

print(s1 > s1)
# False

Determination of whether they are mutually prime or not: isdisjoint()

To determine if two sets are prime to each other, use the isdisjoint() method.

s1 = {0, 1}
s2 = {1, 2}
s3 = {2, 3}

print(s1.isdisjoint(s2))
# False

print(s1.isdisjoint(s3))
# True