Sign in · Sign up

Lesson 5.7

A set keeps only unique items. Duplicates simply don't get in:

unique = set([1, 2, 2, 3])
print(unique)   # {1, 2, 3}

A set is written in curly braces, and set() turns a list into one. The order of the items in a set is not guaranteed: print may show them in another order, and that is fine.

A basic check: 2 in unique asks whether the item is in the set.

Careful: {} is an empty dictionary; an empty set is written set().

The list of numbers values is set by the check. Print, in order:

  1. the set of these numbers — with set;
  2. how many items it has — with len;
  3. True if it contains the number look.
Hint

set(list) removes duplicates; an empty set is set(), while {} is a dictionary.

Print the set, len(...) and the answer x in set: it gives True or False.