10/40
Part 2 of 7 · KeysLearn and do

A tuple holds a key with parts, and a frozenset one with no order

Many keys have more than one part: a grid cell has a row and a column, and a file has a size and a digest. Two hashable containers hold such keys, and they differ on one thing, order.

print(("ann", "bo") == ("bo", "ann"))
print(frozenset({"ann", "bo"}) == frozenset({"bo", "ann"}))
False
True

When a key has parts, like a grid cell's row and column or a file's size and digest, put them in a tuple: (row, col), (size, digest).

A tuple keeps its order, so ("ann", "bo") and ("bo", "ann") are different keys. When order shouldn't matter, as with a friendship or an undirected edge, use a frozenset, the immutable set. The two frozensets above are equal, and they hash the same.

A frozenset keeps each name once: frozenset(("ann", "ann")) holds one name. When repeats matter, use a sorted tuple, tuple(sorted(pair)), instead.

In the editor, write distinct_friendships(pairs): how many different friendships a log of name pairs holds. ("ann", "bo") and ("bo", "ann") are the same friendship, so the key must ignore order.