×

python · level 2 · unit 2 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A keeps exactly one of each value.

A set is a collection that never holds a . Add the same value twice and it is kept once. set(visitors) turns a list into a set, so every repeat vanishes.

A set has no fixed order, so the order it prints in can change from run to run. Pass it to sorted() for a tidy list. .add() puts in one more value, and in checks for one very quickly.

Two sets can be compared: a & b holds what is in both, a | b holds everything from either, and a - b holds what is in a but not in b.

visitors = ["Ana", "Raj", "Ana", "Kim", "Raj"]
unique = set(visitors)
unique.add("Tom")
print(len(unique))
print(sorted(unique))
print("Kim" in unique)

An empty set is written set(), because {} makes an empty dictionary.