Learn · Run · Break · Fix
A learning series from A Wandering Mind
Eighteen interlinked lessons on strings, lists, files, exceptions, and dictionaries—with original explanations, runnable examples, real output, and supporting CPSC 1301K lectures.
>>> guide = { ... "lessons": 18, ... "modules": 5, ... "python": "3.10+" ... } >>> guide["goal"] = "understand it"
List or Dictionary? Choosing the Right Python Collection
Lists and dictionaries can both hold multiple values, but they organize those values around different questions. A list is naturally about sequence and position. A dictionary is naturally about labels and relationships between keys and values. The final lesson of this guide turns that distinction into a practical decision-making tool and shows how the two structures can also work together.
Choose a list when position and sequence matter
A list stores values in an ordered sequence. Each element has a numeric position, starting at index 0. That makes a list a natural choice when your program cares about first, next, last, before, after, or a repeated series of similar values.
Examples include a playlist, a sequence of temperatures, a queue of tasks, steps in a process, or a collection of search results. A list also permits duplicate values, which is useful when repetition is meaningful rather than accidental.
cities = ["Savannah", "Macon", "Atlanta"]
print(cities[0])
print(cities[2])
Savannah
Atlanta
The program is asking positional questions: which city comes first, and which appears at index two? The numeric indexes are meaningful because the collection is a sequence.
List question: “Which value is at this position?” If that is the question your program naturally asks, a list is often the right starting point.
Choose a dictionary when meaningful labels matter
A dictionary maps keys to values. Instead of remembering that a person’s name happens to be at position zero and their level happens to be at position two, code can use descriptive keys such as "name" and "level".
This makes dictionaries natural for records, settings, lookup tables, counters, and structured objects whose fields have distinct meanings.
profile = {"name": "Ada", "language": "Python", "level": 3}
print(profile["name"])
print(profile["level"])
Ada
3
The values are not interchangeable positions in one sequence. They describe different attributes of the same record. The keys make those relationships explicit.
Duplicates are another clue
Lists can naturally contain the same value more than once. If a task log records "email" twice, those two appearances can represent two separate events. Dictionary keys, by contrast, are unique within one dictionary. Assigning the same key again updates its value instead of creating a second copy of that key.
tasks = ["email", "meeting", "email"]
print(tasks)
print(tasks.count("email"))
['email', 'meeting', 'email']
2
The repeated "email" values are legitimate elements of the sequence. A dictionary could count them—such as {"email": 2, "meeting": 1}—but that would represent a different idea: a mapping from task name to count rather than the original event sequence.
Ask how the program will retrieve the data
Choosing a collection is easier when you imagine the operations that will happen later. If the program repeatedly needs “the third item,” “the first five,” or “every item in order,” a list aligns naturally with those operations. If the program repeatedly needs “the value for this name,” “does this key exist?” or “update the setting called theme,” a dictionary aligns naturally with those operations.
Do not choose based only on how the literal looks when typed. Choose based on the relationships in the data and the questions the rest of the program needs to ask.
items[index]Best fit for ordered sequences addressed by numeric position.
data[key]Best fit for mappings addressed by meaningful keys.
items[1:4]Slicing and positional traversal are central list operations.
data.get(key)Named lookup and key-based access are central dictionary operations.
Lists and dictionaries often belong together
Real programs do not have to pick one collection type for everything. A list can contain dictionaries when you have a sequence of structured records. A dictionary can contain lists when one labeled field naturally holds several related values.
This combination is common because it preserves both kinds of meaning: the outer structure describes how records relate to one another, while the inner structure describes how fields inside each record relate.
student = {
"name": "Grace",
"scores": [92, 95, 98]
}
print(student["name"])
print(student["scores"][1])
print(sum(student["scores"]) / len(student["scores"]))
Grace
95
95.0
The dictionary gives the fields meaningful names. The "scores" value is a list because those scores form a sequence of similar values. The best design is sometimes not “list versus dictionary,” but “which structure belongs at each level?”
A simple decision framework
When you are unsure, describe the collection in a sentence. If you say “a sequence of…” or “an ordered set of repeated items,” start by considering a list. If you say “a record with fields…” or “a mapping from one thing to another,” start by considering a dictionary.
Then test that choice against the operations you expect. Will you slice it? Will numeric positions be meaningful? Will duplicate entries matter? Or will most operations begin with a known label or identifier? The data structure should make the common operations easy to express and the relationships easy for another programmer to understand.
Common mistakes when the mental model does not match the collection
The following tracebacks were executed in Python. Each one comes from using an operation associated with one collection type on the other.
cities = ["Savannah", "Macon", "Atlanta"]
print(cities["first"])
Traceback (most recent call last):
File "/tmp/ipykernel_313/3232733075.py", line 12, in run_err
exec(compile(code, filename, "exec"), {})
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/awm_lesson18/mistake1_string_index_list.py", line 2, in <module>
TypeError: list indices must be integers or slices, not str
Lists are addressed by integer indexes or slices, not descriptive string keys. If labels such as "first" are central to the model, reconsider whether a dictionary better represents the data.
profile = {"name": "Ada", "language": "Python"}
print(profile[0])
Traceback (most recent call last):
File "/tmp/ipykernel_313/3232733075.py", line 12, in run_err
exec(compile(code, filename, "exec"), {})
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/awm_lesson18/mistake2_numeric_key_dict.py", line 2, in <module>
KeyError: 0
The dictionary contains string keys, not a key named 0. Insertion order does not turn dictionary lookup into list indexing.
cities = ["Savannah", "Macon", "Atlanta"]
print(cities.items())
Traceback (most recent call last):
File "/tmp/ipykernel_313/3232733075.py", line 12, in run_err
exec(compile(code, filename, "exec"), {})
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/awm_lesson18/mistake3_items_on_list.py", line 2, in <module>
AttributeError: 'list' object has no attribute 'items'
items() belongs to dictionaries because dictionaries contain key/value mappings. A list element does not automatically have a paired key.
profile = {"name": "Ada"}
profile.append("Python")
Traceback (most recent call last):
File "/tmp/ipykernel_313/3232733075.py", line 12, in run_err
exec(compile(code, filename, "exec"), {})
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/awm_lesson18/mistake4_append_on_dict.py", line 2, in <module>
AttributeError: 'dict' object has no attribute 'append'
Dictionaries add entries through key assignment rather than list-style append(). The correct operation follows from the collection’s model.
Supporting classroom explanation
This page is original A Wandering Mind instructional material. The final assigned CPSC 1301K lecture reinforces the design decision at the center of this lesson: whether a collection should be represented as a sequence or a key/value mapping.
What it adds: a focused classroom comparison of lists and dictionaries and the kinds of problems each collection represents naturally.
Try it yourself
For each exercise, state why the chosen collection fits before you run the answer. The reason matters as much as the syntax on this final page.
Exercise 1 · Keep an ordered color sequence
Store "red", "green", and "blue" in a collection where order matters. Print the second value.
Show one answer
colors = ["red", "green", "blue"]
print(colors[1])
green
Exercise 2 · Store labeled camera fields
Store the brand "Nikon" and model "Z9" with meaningful labels, then retrieve the model.
Show one answer
camera = {"brand": "Nikon", "model": "Z9"}
print(camera["model"])
Z9
Exercise 3 · Combine both structures
Create a sequence of two course records. Each record should contain a course code and grade. Print the first course code and the second course grade.
Show one answer
courses = [
{"code": "CS101", "grade": 92},
{"code": "MATH110", "grade": 88}
]
print(courses[0]["code"])
print(courses[1]["grade"])
CS101
88
You finished the A Wandering Mind Python Guide
Across 18 lessons, you have worked with formatted strings, splitting and joining, list creation and mutation, slicing, references, loops, file I/O, exception handling, and dictionaries. More importantly, you have practiced a repeatable way to learn Python: run small examples, inspect exact output, deliberately break code, read the traceback, and then explain why the corrected version works.
This guide is a foundation rather than an endpoint. From here, good next topics include functions, modules, classes, testing, working with JSON and APIs, databases, and larger projects that combine several of the ideas you have already learned.
Your next step is the Test Your Knowledge practice assessment: 30 self-grading questions covering all five modules, with explanations and lesson-specific review links. You can also return to the Python Guide overview at any time.
