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


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.

LanguageNameSyntax
PythonList[1, 2, 3]
JavaScriptArray[1, 2, 3]
JavaArray / ArrayList{1, 2, 3}
GoSlice[]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 32

Key-value pairs are ideal when you need to look up data by a meaningful identifier rather than a position number.

LanguageNameSyntax
PythonDictionary{"key": "value"}
JavaScriptObject{key: "value"}
JavaHashMapmap.put("key", "value")
GoMapmap[string]string{"key": "value"}
RubyHash{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:

StructureWhat it doesWhen to use it
StackLast in, first out (LIFO)Undo operations, function calls
QueueFirst in, first out (FIFO)Task scheduling, message processing
SetUnordered collection of unique valuesRemoving duplicates, membership testing
TreeHierarchical structureFile systems, HTML documents, search
GraphNetwork of connected nodesSocial 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

ConceptWhat it coversStatus
variablesThe individual values that structures containcomplete
data-typesWhat kinds of values can go inside structurescomplete
functionsOperations that create, transform, and query structurescomplete
databasesPersistent data structures that outlive a programcomplete

Check your understanding


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:#fff

Related 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

Footnotes

  1. Cormen, T. et al. (2009). Introduction to Algorithms. 3rd ed. MIT Press.

  2. 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.

  3. Wikipedia. (2026). Array (data structure). Arrays have been fundamental since FORTRAN (1957).

  4. Wikipedia. (2026). Associative array. “Associative arrays can be implemented in any programming language.”

  5. Wikipedia. (2026). String (computer science). “Strings are such an important and useful datatype that they are implemented in nearly every programming language.”