MEPX
Chapter 8 of 15All chapters

Chapter 8 of 15

Tuples and sets

Frozen sequences and unique collections.

Tuples

A tuple is an ordered sequence that cannot change once created. That makes it safe to use as a dictionary key or a set member, which a list can never be.

  • Returning several values from a function returns a tuple.
  • A single item tuple needs the trailing comma: (1,) not (1).

Sets

A set holds unique values with no order. Membership tests are fast, and the operators do the maths: | for union, & for intersection, - for difference.

  • set(items) is the quickest way to remove duplicates.
  • Because there is no order, you cannot index a set.