Data Structures
A data structure is a way of organising values in memory so that the operations you need are simple and fast. The wrong one leads to slow, awkward workarounds.
The everyday five
| Structure | What it gives you | Typical use |
|---|---|---|
| List / array | Ordered items, access by position, duplicates allowed | Results of a query, rows to display, anything you loop over |
| Map / dictionary | Look up a value by a unique key | Find a user by id, count words, cache results |
| Set | Unique items, fast “is it in here?” | Remove duplicates, track visited ids, compare two groups |
| Stack | Last in, first out (LIFO) | Undo history, nested calls, back navigation |
| Queue | First in, first out (FIFO) | Jobs waiting for a worker, messages, breadth-first work |
Names differ by language (dict, Map, HashMap, object), but the ideas carry over.
Choosing between them
Ask what you do most often with the data.
- Loop over everything in order: a list.
- Find one item by an identifier: a map. Searching a list for it means checking items one by one.
- Ask “have I seen this before?”: a set, or a map when you also need data attached.
- Process the oldest item first: a queue. Process the newest first: a stack.
A common beginner slowdown is calling a “contains” check on a list inside a loop. Copying the list into a set first often turns a slow job into a fast one. See Time and Space Complexity.
# Python: count how often each word appears
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1
Keys, order, sharing and queues
- Maps and sets need keys that can be hashed or compared. Mutable objects (a list in Python) usually cannot be keys.
- Do not rely on ordering unless the type promises it.
- Two variables can point to the same list, so changing one changes the other. Copy explicitly when you need independence. See State Management.
- Removing from the front of a plain list is slow in many languages. Use a proper queue or deque type.
Beyond the basics
Know that these exist, and look them up when a problem calls for them:
- Tree: hierarchical data such as folders, a page’s elements or a sorted index.
- Heap / priority queue: always gives the smallest or largest item next.
- Graph: things and the connections between them, such as routes or dependencies.
Databases use similar ideas to find rows fast; see Indexing Basics.
Try it
Write a small script that reads a text file and prints the five most common words. First use a list and nested loops, then a map. Time both on a large file and note the difference.
Common mistakes
- Calling a contains check on a list inside a loop instead of using a set.
- Relying on ordering when the type does not promise it.
- Using a mutable object, such as a Python list, as a map key.
- Removing from the front of a plain list instead of using a queue or deque.
Check yourself
- Which structure suits finding a user by id, and why?
- Why can copying a list into a set speed up a loop?
- What is the difference between a stack and a queue?