Basic

Why data structures matter

Understand how computers organize information and why choosing the right structure makes programs fast.

What you will see

103 101

About 2 min of reading

Try It

A data structure is a layout for storing data, and the layout changes how fast you work with it. I compare a direct position jump against a slow scan, then look a value up by key to see why an organized structure wins.

1

I create one list and grab the first and last entries at the same time, showing how an array jumps straight to any position by its index instead of searching.

names = ["Ada", "Bob", "Cy"] print(names[0], names[2])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada Cy
Indexes start at 0, so the first item is at position 0. That off-by-one detail trips everyone up at first.
2

I find an item by its value, checking each one until it matches. This is the messy desk, it always works but is slow on big lists.

names = ["Ada", "Bob", "Cy"] found = None for item in names: if item == "Cy": found = item break print(found)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Cy
A search scans one by one. On a big list that is slow; a better structure skips straight to the item.
3

I build an organized shelf that keys straight to a value with no scanning at all, then look two names up at once to show the direct jump.

roster = {"Ada": 101, "Bob": 102, "Cy": 103} print(roster["Cy"], roster["Ada"])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
103 101
A hash map turns the key into a slot directly. Lookups by key are much faster than scanning a list.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Data is chosen into a structure based on what operations are needed, then used with the speed that structure offers.
1 Describe what you need to do with the data
↓
2 Pick a structure that supports those operations
↓
3 Store the data in that structure
↓
4 Access it with the structure's natural speed
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

names = ["Ada", "Bob", "Cy"] sets nothing on screen
print(names[0], names[2]) Ada Cy
Why this works
What is a data structure?

A data structure is a way of storing and organizing data so it can be used efficiently. Two broad families:

  • **Linear*: items in a sequence (arrays, lists, stacks, queues).
  • **Hierarchical*: items in a tree-like or network layout (trees, graphs).

Linear:   A - B - C - D
Hierarchical:
        A
       / \
      B   C

Picking the right one is a balance between how fast you read, add, or delete data.

Time complexity in plain words

We measure how a structure's speed scales as data grows. Think of it as "how many things do I have to check?"

StructureFind one item
Array (unsorted)check many
Hash mapjump straight to it

10 items   -> both are fast
10,000     -> hash map wins big
1,000,000  -> the difference is huge

Big structures save the biggest wins, which is why programmers care.

Examples

A messy desk vs an organized one

Organizing your workspace

Imagine looking for a file on a desk covered in papers. It is slow. On an organized shelf with labels, you find it fast. Data structures are the shelf.

Messy desk  ->  search everything  ->  slow
Organized   ->  jump straight to it  ->  fast

Why it works: the same data can be stored in different layouts, and the layout changes how fast you can work with it.

Same data, different needs

Choosing a container

You have a list of student names. Do you need to:

  • Add a new student often? -> a list is easy.
  • Find a student by ID quickly? -> a map is fast.
  • Process the newest entry first? -> a stack.

What will you DO with the data?
   -> that decides the structure.

There is no single "best" structure, only the best one for your job.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Arrays & lists
Arrays & lists →