Concepts / Lists vs. Dictionaries: Choosing the Right Data Structure

Lists vs. Dictionaries: Choosing the Right Data Structure

A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.

  • Programming

Two Ways to Find Data

When you store several pieces of information, the most useful data structure depends on how you plan to find those pieces later. A list organizes items by position. A dictionary organizes information as a mapping between keys and values. The central question is whether you want to retrieve an item by counting to a numeric position or by providing a meaningful label.

Key point

A list answers the question, “What item is at this position?” A dictionary answers the question, “What value belongs to this key?”

Listposition-based0first itemnameDictionarykey-basednamekeyAdaassociated valueaccess withretrievesaccess withmaps to
What is the difference between finding an item by its numeric position in a list and finding an item by its key in a dictionary?

Following a List Position

A list is a sequence of items arranged in positional order. To access an item, you provide an integer index. For example, the first item is retrieved with position 0. The number identifies where the item occurs in the sequence; it does not describe what the item means.

0name1age1requested positionageretrieved item2emailselectsreturns
How does a list store items in positional order, and what happens when an item is accessed by its index?
Worked example

Remembering Positions

Suppose a person's information is stored in a list with the name at position 0, the age at position 1, and the email at position 2. How would someone find the email?

  1. 1
    Identify the required item

    The person needs the email, but the list does not use the word email as an index.

  2. 2
    Recall the position

    The person must remember that the email was placed at position 2.

  3. 3
    Use the numeric position

    The lookup uses the integer position 2 rather than a descriptive label.

Result

The item can be retrieved if the position is known, but the position itself does not explain what the item represents.

Following a Dictionary Key

A dictionary is a mapping between keys and values rather than a sequence indexed by position. A key is a label used to identify the value associated with it. When you provide the key, the dictionary locates and returns its corresponding value directly.

namekeyAdavalueagekey30valueemailkeyada@example.comvaluemaps tomaps tomaps to
How does a dictionary use a key to find and retrieve its corresponding value?
Definition

A key-value pair is the fundamental unit of a dictionary. The key identifies an entry, and the value is the data associated with that key. Each key maps to exactly one value.

Think of a phone book: a person's name acts as the key, and the person's phone number acts as the value. You use the name to find the associated number. A dictionary applies the same relationship to program data, allowing the label and the information it identifies to stay together.

Meaningful Labels

Dictionary keys can be almost any type, including strings, numbers, and tuples, while list indices must be integers. This lets a dictionary use labels that describe what its values represent. Names, categories, and field labels can make the intended meaning of stored data immediately clearer than a sequence of numeric positions.

Worked example

Choosing Labels for Student Data

You need to organize a student's ID and grade. Should the identifying labels be represented only by remembered positions, or should the data use meaningful labels?

  1. 1
    Consider a positional design

    A list can place the ID at one integer position and the grade at another, but a reader must remember which position represents each field.

  2. 2
    Consider a mapping design

    A dictionary can use the labels student_id and grade as keys, so each value is connected to a description of what it represents.

  3. 3
    Choose based on access

    If the data will be requested by field meaning rather than by sequence position, the dictionary design communicates the purpose of each value more clearly.

Result

Use a dictionary when the index itself should carry meaning, such as a student_id or grade label.

QuestionListDictionary
What identifies an item?An integer positionA key
What is the basic organization?A sequenceA mapping between keys and values
What does the index communicate?Where the item occursWhat the associated value represents
What must the reader remember?Which position contains each kind of dataWhich key names the desired data
When is it especially useful?When positional order mattersWhen meaningful labels or non-integer indices matter

The choice depends on whether access is positional or meaning-based.

Common Selection Mistakes

  • Common mistake

    Treating a dictionary as if it were a list with descriptive positions.

    What it looks like

    Assuming that a dictionary's key is simply another way to count to an item.

    Why it fails

    A dictionary is a mapping, not a sequence indexed by position. Its key identifies the associated value directly.

    Do this instead

    Ask whether the lookup should use a position number or a meaningful key.

  • Common mistake

    Using a list for labeled fields and expecting the positions to explain themselves.

    What it looks like

    Storing a student ID and grade at separate positions without communicating which position represents which field.

    Why it fails

    The reader must memorize the relationship between each integer position and the data it contains.

    Do this instead

    Use meaningful keys when the index itself carries semantic meaning.

  • Common mistake

    Assuming every dictionary key must be a string.

    What it looks like

    Believing that dictionary keys can only be labels written as words.

    Why it fails

    The source material states that keys can be almost any type, including strings, numbers, and tuples.

    Do this instead

    Remember that dictionary keys are flexible, while list indices must be integers.

  • Common mistake

    Forgetting that a key maps to one value.

    What it looks like

    Thinking of a key-value pair as an unconnected collection of data.

    Why it fails

    The key and value form one relationship, and each key maps to exactly one value.

    Do this instead

    Read each dictionary entry as a key identifying its associated value.

Choosing by Access Pattern

EASY

A collection stores a person's name, age, and email. A reader should be able to request the email by its descriptive label without remembering its position. Which data structure is the better fit, and why?

Hints
  • Identify whether the requested item is found by position or by meaning.
  • Recall that dictionaries use keys as labels for associated values.

What do you think happens?

Which structure better matches a lookup for a person's email by the label email?

  • A list

  • A dictionary

Reveal answer
Answer:

A dictionary

The lookup is based on a meaningful key rather than an integer position. The key email directly identifies the associated value.

The important decision is not that one structure is always better. Choose a list when sequential position is the useful way to organize or retrieve items. Choose a dictionary when the data has named fields, categories, or other meaningful keys that should directly identify their values.

Key Takeaways

  1. 1
    A list is an ordered sequence accessed by integer position.
  2. 2
    A dictionary is a mapping between keys and values.
  3. 3
    A key-value pair is the fundamental unit of a dictionary, with each key mapping to exactly one value.
  4. 4
    Dictionary keys can be almost any type, including strings, numbers, and tuples, while list indices must be integers.
  5. 5
    Use a dictionary when meaningful labels make data retrieval clearer than remembered numeric positions.

At a glance

  • Lists organize data by sequential integer positions.
  • Dictionaries organize data through key-value mappings.
  • Each dictionary key identifies exactly one associated value.
  • Meaningful keys make labeled data easier to understand and retrieve.
  • Choose the structure according to whether access is positional or key-based.

More concepts starting with L