Concepts / Data Structures: Lists, Tuples, and Dictionaries

Data Structures: Lists, Tuples, and Dictionaries

Lists, tuples, and dictionaries are data structures that store and organize data; the four patterns describe what a program does with that data.

  • Programming

Four Ways Programs Work

The title of this article names lists, tuples, and dictionaries, but the central idea in the source material is broader: programs are built from four fundamental programming patterns. These patterns describe how instructions execute, how decisions are made, how actions repeat, and how code is packaged for reuse. Lists, tuples, and dictionaries are data structures that store and organize data; the four patterns describe what a program does with that data.

Key point

The four patterns are sequential code, conditional code, repetitive code, and store and reuse.

A Program as a Combined Flow

Imagine a program that processes several values stored in a collection. It may first perform instructions in order, then repeat an operation for each value, make a decision about a particular value, and call a reusable function to carry out a task. These are not four separate kinds of programs. They are four patterns that can appear together in one program and can be nested inside one another.

ProgramSequential stepsinstructions in orderLooprepeat a blockIf decisionchoose a pathFunctionstore and reuseNext program stepbeginsmay containmay includemay callreturns to flow
How do sequential instructions, decisions, loops, and functions connect and control the execution of one complete program?

The diagram shows a possible arrangement, not a required order for every program. A function can contain a loop, a loop can call a function repeatedly, and a conditional can select whether a function is called. Sequential steps provide the order inside each part. This nesting is why recognizing one pattern at a time is useful, but understanding their combinations is essential for reading real programs.

The Four Pattern Roles

PatternMain question it answersObservable featureProgramming problem solved
Sequential codeWhat happens first, next, and after that?Instructions execute in orderCarrying out ordered steps
Conditional codeWhich path should the program take?An if statement makes a decisionChoosing between paths when a condition is true or false
Repetitive codeWhat action should happen multiple times?A loop repeats a blockRepeating work without rewriting the block
Store and reuseHow can this group of instructions be used again?A function packages code for later callsAvoiding duplication and improving maintainability

The four patterns can be identified by the kind of control or organization they provide.

Sequential code is the basic pattern: one instruction follows another in order. Conditional code adds a choice, allowing execution to branch depending on whether a condition is true or false. Repetitive code adds repetition, allowing a block of instructions to run multiple times without being rewritten. Store and reuse packages instructions into functions so the same code can be called whenever it is needed.

Data structures support these patterns by storing and organizing data. Lists, tuples, and dictionaries are collection data structures: they store multiple values together and allow a program to access those values by position or key. For example, a loop might process values in a list, or conditional code might check whether a dictionary contains a particular key. The source identifies these uses but does not define the separate rules or operations of each data structure.

Tracing Repetition and State

Consider a generated scenario: a program has a collection of values and must perform the same checking action for each value. A loop supplies the repetitive pattern. During one repetition, the program can use sequential steps to examine the current value and then use conditional code to choose what happens next. After that repetition ends, the loop moves on to another value until the repeated work is complete.

Loop beginsfirst valueProcess valuerepeated blockNext valueadvanceProcess valuesame block againLoop endsno more valuesenterrepeatcontinuewhen complete
What happens to control flow and program state each time a loop repeats, and when does the loop stop?

A loop changes the control-flow position from the end of its block back to the repeated block or to the next item being processed. The exact loop syntax is not the focus here. The important recognition clue is that a group of instructions is executed multiple times. Sequential instructions and conditional decisions can occur inside each repetition.

Choosing Between Paths

Conditional code solves a different problem from repetitive code. A condition does not primarily ask how many times an action should occur. It asks which path should be taken. When the condition is true, one path can run; when it is false, another path can run. This makes if statements the visible clue that a scenario uses the conditional pattern.

Conditiontrue or falseTrue pathchosen when trueContinueFalse pathchosen when falsetruefalseafter branchafter branch
How does the program choose between different execution paths when an if condition is true or false?
In the real world

Suppose a program examines information stored in a dictionary and must choose what to do depending on whether a particular key is present. The key check is conditional code. If the key is present, the program follows one path; if it is not present, the program follows another. The dictionary supplies organized data, while the if decision controls the path.

Packaging Work for Reuse

Store and reuse addresses duplication. A function packages instructions into a reusable unit that can be called whenever the task is needed. This pattern helps make a program easier to maintain because the instructions are organized in one reusable place rather than copied into many locations.

Calling coderequests a taskFunctionpackaged instructionsFunction stepsmay include patternsCalling codecontinuescallexecutefinish
How does control move into a function, use its packaged instructions, and return to the calling code?

A function is not a replacement for the other patterns. Its packaged instructions can contain sequential steps, conditional branches, or loops. Likewise, a loop can call a function more than once. The function provides organization and reuse, while the other patterns provide order, choice, and repetition.

Recognizing Patterns in Scenarios

To identify a pattern, look for the programming problem being solved rather than focusing only on the data structure. Ask whether the scenario is mainly ordering instructions, choosing a path, repeating a block, or packaging instructions for reuse. A list, tuple, or dictionary may appear in any of these situations, but its presence does not by itself identify the control-flow pattern.

Scenario cluePattern to recognizeReason
One instruction follows anotherSequential codeThe program is executing steps in order
An if statement selects a pathConditional codeThe program is making a true-or-false decision
A block runs multiple timesRepetitive codeThe program is repeating instructions
Instructions are packaged for later callsStore and reuseThe program is organizing code into a function
Multiple values are kept togetherData structure useThe program is storing and organizing data

Use the observable action in the scenario to identify the pattern.

EASY

Classify each description as sequential, conditional, repetitive, store and reuse, or data structure use. A program performs three ordered preparation steps. A program checks whether a dictionary contains a key and selects a path. A program applies the same operation to values in a list. A program places a group of instructions inside a function so it can be called again.

Hints
  • Look for order in the first description.
  • Look for a true-or-false choice in the second.
  • Look for repeated work in the third.
  • Look for packaged instructions in the fourth.

Common Recognition Mistakes

  • Common mistake

    Treating lists, tuples, or dictionaries as one of the four programming patterns.

    What it looks like

    A scenario is labeled repetitive only because it uses a list.

    Why it fails

    The source describes these as data structures for storing and organizing data. Repetition comes from the loop, not merely from the collection.

    Do this instead

    Identify the action involving the data: ordered execution, a decision, repeated work, or packaged reusable instructions.

  • Common mistake

    Calling every multi-step program sequential and stopping the analysis there.

    What it looks like

    A program processes several values and is described only as sequential.

    Why it fails

    Although the steps inside the program may execute in order, the program may also contain a loop, a conditional, or a function.

    Do this instead

    Check whether the steps repeat, branch, or are packaged for reuse.

  • Common mistake

    Assuming a function always represents only store and reuse.

    What it looks like

    A function that contains a loop is classified as repetitive but not store and reuse.

    Why it fails

    Patterns can nest. A function can package instructions while those instructions also include a loop or an if statement.

    Do this instead

    Recognize both levels: the function provides store and reuse, and the loop or decision inside it provides another pattern.

  • Common mistake

    Describing a loop as a decision because it controls whether execution continues.

    What it looks like

    A repeated block is labeled conditional simply because the loop eventually stops.

    Why it fails

    The defining feature of repetitive code is executing a block multiple times. Conditional code is defined by choosing between paths based on whether a condition is true or false.

    Do this instead

    Ask whether the main behavior is choosing a path or repeating a block.

From Patterns to Larger Programs

A useful way to design, debug, or extend a program is to label its patterns. First identify the sequential steps. Then mark decisions, repeated blocks, and functions. Finally, inspect how those pieces nest. This gives you a structural view of the program: data structures organize the values, while the four patterns organize the program's actions and control flow.

These patterns are also foundational for object-oriented programming. The source describes classes as building on the same ideas: a class packages related code and data, methods use combinations of the four patterns, and loops or conditional choices can operate on collections of objects. Learning the four patterns therefore prepares you to understand larger ways of organizing programs.

Essential Takeaways

  1. 1
    Sequential code executes instructions in order.
  2. 2
    Conditional code uses if statements to choose between paths.
  3. 3
    Repetitive code uses loops to run a block multiple times.
  4. 4
    Store and reuse packages instructions into functions that can be called again.
  5. 5
    Lists, tuples, and dictionaries store and organize multiple values and can be used within any of the four patterns.
  6. 6
    Real programs combine and nest the patterns rather than using them in isolation.

At a glance

  • The four fundamental programming patterns are sequential, conditional, repetitive, and store and reuse.
  • Each pattern solves a different problem: order, choice, repetition, or code organization and reuse.
  • Lists, tuples, and dictionaries organize data, but they are not substitutes for control-flow patterns.
  • Patterns can combine and nest, such as a function containing a loop with a conditional inside it.
  • Recognizing these patterns helps you design, debug, and extend programs.

Related concepts

More concepts starting with D