Set: Membership Array

05_membership_array

Python Sets Are Membership Arrays

  • Sets are homogeneous collections of unique values.
  • A literal set is defined like a list – except with curly-brackets not square-brackets.
  • The sorted order of a set is maintained automatically in some languages, in Python however, the set is unordered. If you care about the order of elements – you should use a sequential collection type.
  • A set can only contain unique hashable values. Duplicates are correctly ignored without error.
  • Sets support common Set Theory operations like union.
  • Sets are especially good for membership testing.

Python Set | python.org

In [1]:
my_set = {3, 4, 1}
print(my_set)
{1, 3, 4}

Adding and removing elements in a set is simple. However, the add method is distinctly not the same as append or insert on a list. While the list allows you to place items at arbitrary positions – the set does not. The set will manage the order of values for you. Due to this sublte difference, it would break polymorphism to name the add method insert or append. Polymorphism will be discussed in the class module.

One method that the set and list share is the pop method, and it works exactly the same.

In [2]:
my_set.add(2)
my_set.remove(1)
print(my_set)
{2, 3, 4}

Sets can only contain hashable values. Since lists are not hashable, the following code will raise a TypeError…

# Don't do this
list_set = {
    [1, 2, 3],
    [3, 4, 6],
    [3, 2, 1],
}
TypeError: unhashable type: 'list'

While lists cannot live inside a set, tuples can!

In [3]:
tuple_set = {
    (1, 2, 3),
    (4, 5, 6),
    (9, 8, 7),
}
print(tuple_set)
{(4, 5, 6), (9, 8, 7), (1, 2, 3)}

Notice that the sorted order of tuple_list below, differs from the order of tuple_set above. Why might this be?

In [4]:
tuple_list = [
    (1, 2, 3),
    (4, 5, 6),
    (9, 8, 7),
]
print(sorted(tuple_list))
[(1, 2, 3), (4, 5, 6), (9, 8, 7)]

Set Theory Operations:

Sets have special operations derived from set theory. Python implements these operations as methods on the set object. We’ve seen this before with string methods.

Each of the following methods are non-destructive and will return a new set when called.

  • Union
  • Intersection
  • Difference
  • Symetric Difference
In [5]:
some_set = {1, 2, 3}
another_set = {3, 4, 5}

print(some_set)
print(another_set)
{1, 2, 3}
{3, 4, 5}
In [6]:
union_of_sets = some_set.union(another_set)
print(union_of_sets)
{1, 2, 3, 4, 5}
In [7]:
intersect = some_set.intersection(another_set)
print(intersect)
{3}
In [8]:
difference_a = some_set.difference(another_set)
print(difference_a)
{1, 2}
In [9]:
difference_b = another_set.difference(some_set)
print(difference_b)
{4, 5}
In [10]:
sym_dif = some_set.symmetric_difference(another_set)
print(sym_dif)
{1, 2, 4, 5}
In [11]:
# The original sets are unharmed.
print(some_set)
print(another_set)
{1, 2, 3}
{3, 4, 5}

Membership Testing

This is one of the super-powers of the set.

In [12]:
group = {2, 4, 6, 8, 1}
test_a, test_b = 3, 4

print(test_a in group)
print(test_b in group)
False
True
In [13]:
subjects = (1, 2, 3, 4, 5)

for value in subjects:
    if value in group:
        print(f"{value}: Member")
    else:
        print(f"{value}: Not Member")
1: Member
2: Member
3: Not Member
4: Member
5: Not Member