What a data structure is (and why you already use them)
A data structure is just a way of arranging data so that some operation is fast. A list is fast to index and slow to search; a dictionary is fast to search and cannot be indexed by position. An algorithm is a recipe that uses those operations. You have used both since Module 03 — this module makes the trade-offs explicit, because choosing the structure is most of solving the problem.
You should see
list search: much slower than the set
set search: instant"user199999" to "user0" (the first item). Why is the list suddenly fast too? That is why we talk about the worst case.| Structure | Python | Index by position | Search by value | Add / remove at end | Add / remove at front | Ordered? |
|---|---|---|---|---|---|---|
| Array / dynamic array | list | O(1) | O(n) | O(1) | O(n) | yes, by insertion |
| Hash map | dict | — | O(1) by key | O(1) | — | insertion order kept |
| Hash set | set | — | O(1) | O(1) | — | no |
| Stack | list (append/pop) | — | O(n) | O(1) | — | LIFO |
| Queue | collections.deque | O(n) | O(n) | O(1) | O(1) | FIFO |
| Linked list | build it | O(n) | O(n) | O(1) with tail | O(1) | yes |
| Binary search tree | build it | — | O(log n) if balanced | O(log n) | — | sorted |
| Heap | heapq | — | O(n) | O(log n) | min in O(1) | partial |
| Graph | dict of lists | — | BFS / DFS O(V+E) | O(1) | — | no |
