Part 04 / Topic 9 of 11
Choosing the right structure
| Need | Use | Why |
|---|---|---|
| Ordered items you will add/remove | list |
changeable, keeps order, allows repeats |
| A fixed record (point, date, RGB) | tuple |
cannot change by mistake, can be a dict key (if everything inside is hashable) |
| Unique items / fast "is it there?" | set |
removes duplicates, fast in |
| Look up a value by a name/ID | dict |
key → value, fast lookup |
| Feature | list | tuple | set | dict |
|---|---|---|---|---|
| Brackets | [] |
() |
{} / set() |
{k: v} |
| Ordered | ✅ | ✅ | ❌ | ✅ (insertion) |
| Changeable | ✅ | ❌ | ✅ | ✅ |
| Duplicates | ✅ | ✅ | ❌ | keys ❌ |
| Indexing | [0] |
[0] |
❌ | [key] |
Done here? The practice set for Part 4 — 8 exercises with solutions — is on the part page.