Latticework

Command Palette

Search for a command to run...

Python

Collections

14 min

Explanation

Python has four built-in collection types, each with a different shape:

| Type | Ordered | Mutable | Duplicates | Syntax | |---|---|---|---|---| | list | yes | yes | allowed | [1, 2, 3] | | tuple | yes | no | allowed | (1, 2, 3) | | set | no | yes | not allowed | {1, 2, 3} | | dict | yes (3.7+) | yes | keys unique | {"a": 1} |

tuple's immutability makes it usable as a dict key or set element — something a list can never do, since dict keys and set elements must be hashable, and mutable objects generally aren't.

Try it

Sets support the same operators as mathematical set theory — union, intersection, and difference are all O(min(len(a), len(b))) on average, much faster than nested loops over lists.

Loading editor…
Explanation

Reach for each one based on what you actually need:

  • list — an ordered sequence you'll iterate, index, or append to.
  • tuple — a fixed, small group of values (like coordinates (x, y)), or anything you need to use as a dict key / set element.
  • set — you only care about membership ("is x in here?") and uniqueness, not order — in is O(1) average vs O(n) for a list.
  • dict — you're mapping keys to values, or counting/grouping things.
Exercise

Write a function `unique_preserve_order(items)` that returns a list of the unique elements in `items`, keeping the order of their first occurrence.

Exercise

Write a function `common_elements(a, b)` that returns a sorted list of the elements present in both `a` and `b`. Use set operations, not nested loops.

Quiz

Which Python built-in collection type is both ordered and immutable?

Checkpoint

You know when to reach for list vs tuple vs set vs dict, and can use set operations for membership/intersection problems.