Lesson 100 +10 XP

Cheatsheet: Collections Compared

Cheatsheet: Collections Compared

List, tuple, set, dict, and frozenset at a glance.

TypeOrderedChangeableDuplicatesSyntax
listYesYesAllowed[ ]
tupleYesNoAllowed( )
setNoYes (add/remove)No{ }
frozensetNoNoNofrozenset()
dictYesYesNo keys{k: v}

Quick memory hooks

  • Need to change it? Use a list.
  • Need it to never change? Use a tuple.
  • Need unique values fast? Use a set.
  • Need to look things up by name? Use a dict.

Access style

  • list, tuple: by index.
  • set: no indexing; membership only.
  • dict: by key.

TL;DR

  • Lists are the flexible workhorse.
  • Tuples protect data.
  • Sets hold unique items fast.
  • Dicts map keys to values.
  • frozenset is the unchangeable set.