A data structure is a particular way of organizing data so that certain operations, like lookup, insertion or ordering, are efficient. Common ones are arrays, linked lists, stacks, queues, hash maps, trees and graphs.
Why it matters
The right structure can turn a slow program into a fast one. Looking up a key in a hash map is typically constant time on average, while scanning a list takes time proportional to its length. Each structure trades speed, memory and flexibility differently, and an algorithm is usually chosen together with the structure it works on.
Example
| Structure | Good at | Weak at |
|---|---|---|
| Array | Access by index | Inserting in the middle |
| Hash map | Lookup by key | Keeping sorted order |
| Stack | Last in, first out | Random access |
| Queue | First in, first out | Random access |
seen = set() # hash-based: fast membership test
if user_id in seen: ...
Common mistakes
- Using a list to check membership repeatedly when a set or dictionary would do.
- Forgetting that mutable structures shared between functions can be changed unexpectedly.
- Picking an exotic structure when an array or map is enough.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.