Loading lessons...
Cheatsheet: Collections Compared
Cheatsheet: Collections Compared
List, tuple, set, dict, and frozenset at a glance.
| Type | Ordered | Changeable | Duplicates | Syntax |
|---|---|---|---|---|
| list | Yes | Yes | Allowed | [ ] |
| tuple | Yes | No | Allowed | ( ) |
| set | No | Yes (add/remove) | No | { } |
| frozenset | No | No | No | frozenset() |
| dict | Yes | Yes | No 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.