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.
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.
A list answers the question, “What item is at this position?” A dictionary answers the question, “What value belongs to this key?”
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.
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?
Identify the required item: The person needs the email, but the list does not use the word email as an index.
Recall the position: The person must remember that the email was placed at position 2.
Use the numeric position: The lookup uses the integer position 2 rather than a descriptive label.
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.
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.
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?
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.
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.
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.
Use a dictionary when the index itself should carry meaning, such as a student_id or grade label.
| Question | List | Dictionary |
|---|---|---|
| What identifies an item? | An integer position | A key |
| What is the basic organization? | A sequence | A mapping between keys and values |
| What does the index communicate? | Where the item occurs | What the associated value represents |
| What must the reader remember? | Which position contains each kind of data | Which key names the desired data |
| When is it especially useful? | When positional order matters | When meaningful labels or non-integer indices matter |
Common Selection Mistakes
Treating a dictionary as if it were a list with descriptive positions.
A dictionary is a mapping, not a sequence indexed by position. Its key identifies the associated value directly.
Fix:
Ask whether the lookup should use a position number or a meaningful key.Using a list for labeled fields and expecting the positions to explain themselves.
The reader must memorize the relationship between each integer position and the data it contains.
Fix:
Use meaningful keys when the index itself carries semantic meaning.Assuming every dictionary key must be a string.
The source material states that keys can be almost any type, including strings, numbers, and tuples.
Fix:
Remember that dictionary keys are flexible, while list indices must be integers.Forgetting that a key maps to one value.
The key and value form one relationship, and each key maps to exactly one value.
Fix:
Read each dictionary entry as a key identifying its associated value.
Choosing by Access Pattern
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?
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
- A list is an ordered sequence accessed by integer position.
- A dictionary is a mapping between keys and values.
- A key-value pair is the fundamental unit of a dictionary, with each key mapping to exactly one value.
- Dictionary keys can be almost any type, including strings, numbers, and tuples, while list indices must be integers.
- Use a dictionary when meaningful labels make data retrieval clearer than remembered numeric positions.
Key Takeaways
- 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.