QuickDevGuide

Home › Python

Lists, Tuples & Sets — Python

Type Ordered Mutable Duplicates Literal Typical use list ✅ ✅ ✅ [1, 2] general sequence tuple ✅ ❌ ✅ (1, 2) fixed records, dict keys, returning multiple values set ❌ ✅ ❌ {1, 2} membership, de-duplication, set algebra frozenset ❌ ❌ ❌ frozenset({1}) hashable set Performance x in list is O(n); x in set is O(1) on average — convert to a set when checking membership repeatedly. Appending to a list is O(1); inserting at the front is O(n) — use collections.deque for queues. Copying b = a copies the reference. Use a.copy() / a[:] for a shallow copy and copy.deepcopy(a) for nested structures. Useful helpers sorted(key=…, reverse=True), min/max(key=…), sum, any/all, reversed, itertools (chain, groupby, combinations), bisect for sorted inserts, heapq for priority queues.

Key Takeaways