Code

Unique Visitors

A set is a bag of unique values, duplicates simply vanish. Sets have no order, so there's no [0] indexing.

seen = set(["ada", "bo", "ada"])
print(len(seen))     # 2
seen.add("cy")
print("bo" in seen)  # True (and very fast!)

Sets can be combined like in maths:

Code Meaning
a | b union: in a or b (or both)
a & b intersection: in both
a - b difference: in a but not b

Because sets have no order, use sorted(s) when you want to print them neatly. It returns a sorted list.

Pip tip: {} makes an empty dict. For an empty set, write set().

Assignment

The lighthouse logs who visited on Monday and Tuesday.

  1. Turn both lists into sets: mon and tue.
  2. everyone: people who visited on either day.
  3. both_days: people who came on both days.
  4. only_monday: people who came Monday but not Tuesday.