Data Structures24 min read

Structure Decision Mastery

Purpose: make you confident in choosing the right data structure without guessing. Benefit: your code becomes faster, cleaner, and easier to maintain by matching the structure to the job (order, uniqueness, lookup, FIFO/LIFO, priority).

David Miller
October 12, 2025
7.7k184

This is the final lesson where you stop thinking:
“list or set? dict or list?”
and start thinking like a problem solver.

A beginner usually chooses structures by habit.
A good programmer chooses by **operations** (what you do most).

## The decision rule (the simplest one)
Ask yourself these questions in order:

1) Do I need **key -> value** lookup?
2) Do I need **uniqueness** (no duplicates)?
3) Do I need **order** (sequence matters)?
4) Do I need **FIFO / LIFO / Priority** behavior?

If you answer these correctly, your structure becomes obvious.

## Quick decision map (visual)
```mermaid
flowchart TD
  A[What do you need?] --> B{Key -> value lookup?}
  B -->|Yes| C[dict]
  B -->|No| D{Need uniqueness?}
  D -->|Yes| E[set]
  D -->|No| F{Need order + indexing?}
  F -->|Yes| G[list]
  F -->|No| H{Fixed, unchangeable data?}
  H -->|Yes| I[tuple]
  H -->|No| J{Need FIFO (first in first out)?}
  J -->|Yes| K[deque queue]
  J -->|No| L{Need LIFO (last in first out)?}
  L -->|Yes| M[stack (list/deque)]
  L -->|No| N{Need highest/lowest priority first?}
  N -->|Yes| O[heapq priority queue]
```

---

# 1) Scenario Decisions (Real-world style)

## Scenario 1: “Keep events in order (timeline / logs)”
### Problem
You are storing events as they happen, and later you display them in the same order.

### Best structure: list
Because:
- order matters
- you append a lot
- you read in order

```python
events = []
events.append({"t": "10:00", "msg": "login"})
events.append({"t": "10:02", "msg": "viewed profile"})
print(events[0]["msg"])  # login
```

**Benefit:** simple, readable, keeps natural order.

---

## Scenario 2: “Track unique users online”
### Problem
Same user may ping your server many times, but you want unique online users.

### Best structure: set
Because:
- duplicates should be removed automatically
- membership check should be fast

```python
online = set()
online.add("Tom")
online.add("Tom")  # duplicate ignored
online.add("Sarah")
print(online)  # {'Tom', 'Sarah'} (order not guaranteed)
print("Tom" in online)  # fast
```

**Benefit:** prevents duplicates, super fast membership.

---

## Scenario 3: “Lookup user profile by id (fast)”
### Problem
You have an id, you want the user data instantly.

### Best structure: dict
Because:
- key -> value lookup
- fast average reads

```python
users = {
  101: {"name": "Tom", "city": "Austin"},
  102: {"name": "Sarah", "city": "Miami"},
}
print(users[101]["name"])  # Tom
```

Safe read:
```python
print(users.get(999, {"name": "Unknown"}))
```

**Benefit:** avoids slow searching in lists.

---

## Scenario 4: “Undo feature (editor, form, user actions)”
### Problem
You want to undo the latest action first.

### Best structure: stack (LIFO)
Because:
- last action should come out first

```python
history = []
history.append("state1")
history.append("state2")
last = history.pop()
print(last)  # state2
```

**Benefit:** perfect natural match for undo.

---

## Scenario 5: “Task processing (jobs in order)”
### Problem
First task added should be processed first.

### Best structure: queue (FIFO) using deque
Because:
- FIFO behavior
- efficient popleft()

```python
from collections import deque

q = deque()
q.append("task1")
q.append("task2")

print(q.popleft())  # task1
```

**Benefit:** real-world job systems use this idea.

---

## Scenario 6: “Urgent job first (priority scheduling)”
### Problem
You don’t want FIFO, you want highest/lowest priority first.

### Best structure: heapq (priority queue)
Because:
- always pops smallest priority item first (or use negative for max)

```python
import heapq

pq = []
heapq.heappush(pq, (1, "urgent"))
heapq.heappush(pq, (5, "low"))
heapq.heappush(pq, (2, "medium"))

while pq:
    p, task = heapq.heappop(pq)
    print(p, task)
```

**Benefit:** used in scheduling, routing, “top-k” problems.

---

# 2) The “Wrong Structure” Mistakes (and why they hurt)

## Mistake A: Using list for membership-heavy checks
```python
nums = list(range(1_000_000))
print(999_999 in nums)  # slow compared to set
```

Fix:
```python
nums_set = set(nums)
print(999_999 in nums_set)  # fast
```

## Mistake B: Using set when you need stable ordering
Sets don’t guarantee order for your logic.  
If order matters, use list (or sort).

---

# 3) A “One Minute Cheat Sheet”
- **list**: ordered, duplicates allowed, indexing needed
- **tuple**: ordered, fixed data (immutable)
- **set**: unique items, fast membership
- **dict**: key -> value mapping, fastest lookup style
- **deque**: queue/stack optimized ends
- **heapq**: priority-based retrieval

---

# Final takeaway (the mindset)
Data structures are not “topics”.
They are “tools”.

Pick the tool based on the job:
- order? list/tuple
- uniqueness? set
- lookup? dict
- FIFO? deque
- LIFO? stack
- priority? heapq

## Congratulations
You completed the intermediate level Data Structures playlist (35 tutorials).
#Python#Advanced#Decision Making