Data Structures
Ways of organising multiple pieces of data into a single unit — like arrays (ordered lists), dictionaries (key-value lookups), and strings (character sequences) — so that data can be stored, accessed, and modified efficiently.
What is it?
A single variable holds a single value. But programs rarely work with single values. They work with lists of customers, tables of transactions, collections of settings, sequences of events. Data structures organise multiple values into a coherent unit.1
Choosing the right data structure for a task is one of the most impactful decisions a programmer makes. The wrong choice can make a program 1,000 times slower — not because the code is wrong, but because the structure does not match the access pattern.2
Three data structures exist in virtually every programming language: arrays (ordered lists), key-value pairs (dictionaries), and strings (text sequences). Understanding these three covers the vast majority of real-world programming.
In plain terms
Data structures are organisational systems for information. An array is a numbered row of mailboxes — access by position. A dictionary is a phone book — access by name. A string is a train of carriages — a sequence of characters. Each system is optimised for a different way of finding what you need.
At a glance
The three universal structures (click to expand)
graph TD DS[Data Structures] --> ARR[Arrays / Lists<br/>ordered by position] DS --> KV[Key-Value Pairs<br/>accessed by name] DS --> STR[Strings<br/>character sequences] style DS fill:#4a9ede,color:#fff style ARR fill:#5cb85c,color:#fff style KV fill:#e8b84b,color:#fff style STR fill:#9b59b6,color:#fffKey: These three cover most programming needs. More specialised structures (stacks, queues, trees, graphs) build on these foundations.
How does it work?
Arrays and lists: the numbered row
An array is an ordered collection of items, each accessed by its position number (index). Most languages start counting at 0.3
colours = ["red", "green", "blue"]
# colours[0] is "red"
# colours[1] is "green"
# colours[2] is "blue"Arrays are ideal when order matters and you need to access items by position or iterate through all items sequentially.
| Language | Name | Syntax |
|---|---|---|
| Python | List | [1, 2, 3] |
| JavaScript | Array | [1, 2, 3] |
| Java | Array / ArrayList | {1, 2, 3} |
| Go | Slice | []int{1, 2, 3} |
Think of it like...
A row of numbered mailboxes. Mailbox 0 holds the first item, mailbox 1 the second. To find an item, you go directly to its number. To find a specific item without knowing its number, you check each mailbox in order.
Key-value pairs: the dictionary
A key-value pair maps a unique key (a name) to a value. You look up data by its key, not by its position — like looking up a word in a dictionary to find its definition.4
person = {"name": "Marie", "age": 32, "city": "Lausanne"}
# person["name"] is "Marie"
# person["age"] is 32Key-value pairs are ideal when you need to look up data by a meaningful identifier rather than a position number.
| Language | Name | Syntax |
|---|---|---|
| Python | Dictionary | {"key": "value"} |
| JavaScript | Object | {key: "value"} |
| Java | HashMap | map.put("key", "value") |
| Go | Map | map[string]string{"key": "value"} |
| Ruby | Hash | {key: "value"} |
Five names for the same concept.
Think of it like...
A contact list on your phone. You do not remember that Marie is the 47th contact. You search by name (key) and get her phone number (value). The contact list is organised for lookup by name, not by position.
Strings: the character train
A string is a sequence of characters — text. “Hello” is a string of five characters. Every language provides built-in operations to search, split, join, and transform strings.5
message = "Hello, World"
message.upper() # "HELLO, WORLD"
message.split(", ") # ["Hello", "World"]
message.replace("World", "Marie") # "Hello, Marie"Strings behave like arrays of characters in many contexts — you can access individual characters by index, iterate through them, and check their length.
Beyond the basics
These three structures cover most needs. For specialised tasks, more advanced structures exist:
| Structure | What it does | When to use it |
|---|---|---|
| Stack | Last in, first out (LIFO) | Undo operations, function calls |
| Queue | First in, first out (FIFO) | Task scheduling, message processing |
| Set | Unordered collection of unique values | Removing duplicates, membership testing |
| Tree | Hierarchical structure | File systems, HTML documents, search |
| Graph | Network of connected nodes | Social networks, routes, dependencies |
Why do we use it?
Key reasons
1. Organisation. Raw data is chaos. Structures give it shape. A list of names is more useful than five separate variables called
name1,name2,name3.2. Performance. The right structure makes operations fast. Finding an item in a dictionary by key is instant. Finding it in an unsorted list requires checking every item.
3. Expressiveness. Structures let you model real-world concepts naturally. A person has a name, an age, and a city — that is a key-value pair. A shopping cart has items in order — that is a list.
4. Interoperability. Data formats like JSON are built on arrays and key-value pairs. Understanding these structures is understanding how data moves between systems.
When do we use it?
- Lists/arrays when you have a collection of items in a specific order — shopping carts, search results, time series
- Key-value pairs when you need to look up data by a meaningful name — configuration settings, user profiles, API responses
- Strings whenever you work with text — user input, file content, messages, URLs
- Stacks and queues when the order of processing matters — undo history, job queues, breadth-first search
Rule of thumb
If you need items in order, use a list. If you need items by name, use a dictionary. If you are unsure, start with a dictionary — it is the most versatile structure for structured data.
How can I think about it?
The toolbox
Data structures are like compartments in a toolbox. The main tray (array) holds tools in order — screwdriver first, wrench second, pliers third. The labelled drawers (dictionary) hold tools by name — “screws” drawer, “nails” drawer, “bolts” drawer.
If someone says “hand me the third tool,” you go to the tray (array access by index). If someone says “hand me the screws,” you go to the labelled drawer (dictionary access by key). Both hold tools. The difference is how you find them.
The library
A library organises the same books in multiple structures. The shelves are arrays — books ordered by call number. The card catalogue is a dictionary — look up a title (key) to find its shelf location (value). The checkout system is a queue — first to request, first to receive.
Each structure exists because a different access pattern needs it. No single structure is best for all purposes.
Concepts to explore next
| Concept | What it covers | Status |
|---|---|---|
| variables | The individual values that structures contain | complete |
| data-types | What kinds of values can go inside structures | complete |
| functions | Operations that create, transform, and query structures | complete |
| databases | Persistent data structures that outlive a program | complete |
Check your understanding
Test yourself (click to expand)
- Explain the difference between an array and a dictionary. Give an everyday example of each.
- Describe three operations you can perform on a string in any programming language.
- Distinguish between a stack and a queue. What real-world process does each model?
- Interpret this JSON:
{"users": [{"name": "Marie"}, {"name": "Luc"}]}. What data structures are used and how are they nested?- Connect data structures to memory-management: where do arrays and dictionaries typically live — stack or heap? Why?
Where this concept fits
Position in the knowledge graph
graph TD SD[Software Development] --> DS[Data Structures] SD --> VAR[Variables] SD --> DT[Data Types] VAR --> DS DT --> DS DS -.->|persisted by| DB[Databases] style DS fill:#4a9ede,color:#fffRelated concepts:
- variables — the building blocks inside data structures
- data-types — determines what values a structure can hold
- databases — persistent data structures that outlive program execution
- memory-management — structures live on the heap, which requires allocation and cleanup
Sources
Further reading
Resources
- 8 Data Structures Every Programmer Should Know — Visual overview with real-world use cases for each structure
- Data Structures in Python — Real Python’s practical guide with performance characteristics
- VisuAlgo — Interactive visualisations of data structures and algorithms
Footnotes
-
Cormen, T. et al. (2009). Introduction to Algorithms. 3rd ed. MIT Press. ↩
-
The choice between a list and a hash map can mean the difference between O(n) and O(1) lookup — a 1,000x difference on a list of 1,000 items. Skiena, S. (2020). The Algorithm Design Manual. 3rd ed. Springer. ↩
-
Wikipedia. (2026). Array (data structure). Arrays have been fundamental since FORTRAN (1957). ↩
-
Wikipedia. (2026). Associative array. “Associative arrays can be implemented in any programming language.” ↩
-
Wikipedia. (2026). String (computer science). “Strings are such an important and useful datatype that they are implemented in nearly every programming language.” ↩
