Originally published on my newsletter, The Path to AI FDE. Subscribe free to get each issue by email.
This is a “From Zero” issue. Yesterday you wrapped code in functions. Today you learn to hold many values at once, instead of one variable per value.
Python has two containers you’ll use every day. A list keeps things in order, like a numbered queue. A dictionary keeps things by name, like a form with labelled boxes. Put them together, a list of dictionaries, and you get the exact shape of most data that AI tools send and receive. By the end you’ll have a small script that sums up a made-up playlist.
Part A · Lists
1. A list keeps items in order, and counts from 0. Make a list with square brackets and commas. Save this as lists.py in your sandbox folder:
fruits = ["apple", "banana", "cherry"] print(fruits[0]) print(fruits[2]) print(fruits[-1]) print(len(fruits)) Run it with python3 lists.py (Windows: python lists.py):
apple cherry cherry 3 Each item has a position number called an index, and Python starts counting at 0. So fruits[0] is the first item and fruits[2] is the third. A negative index counts from the end: fruits[-1] is always the last item. len() tells you how many items there are.
Because counting starts at 0, the last index is one less than the length. Ask for fruits[3] and you get:
IndexError: list index out of range That “one too far” slip is called an off-by-one error. It’s the most common list mistake there is.
2. Add, count, slice and sort. Lists can grow and change. Try this:
scores = [7, 3, 9] scores.append(5) print(scores) print(len(scores)) print(scores[1:3]) print(scores[:2]) print(sorted(scores)) print(scores) scores.sort(reverse=True) print(scores) [7, 3, 9, 5] 4 [3, 9] [7, 3] [3, 5, 7, 9] [7, 3, 9, 5] [9, 7, 5, 3] Line by line:
-
.append(5)adds one item to the end. - Slicing,
scores[1:3], gives a new list from index 1 up to but not including index 3. Leave out the first number and it starts at the beginning, soscores[:2]is the first two items. -
sorted(scores)gives back a new sorted list and leaves the original alone, which is why the next line still prints[7, 3, 9, 5]. -
.sort()sorts the list itself.reverse=True(a keyword argument, from yesterday) sorts from biggest to smallest.
One trap: .sort() changes the list and returns None. So result = scores.sort() leaves result as None. If you want a sorted copy, use sorted().
Part B · Dictionaries and friends
3. A dictionary stores values under names, called keys. Use curly braces, with a key, a colon, and a value for each entry:
book = {"title": "Moon Garden", "author": "A. Rao", "year": 2019} print(book["title"]) book["pages"] = 320 book["year"] = 2020 print(book) print(len(book)) Moon Garden {'title': 'Moon Garden', 'author': 'A. Rao', 'year': 2020, 'pages': 320} 4 You look a value up by its key, not by position: book["title"]. Assigning to a new key, book["pages"] = 320, adds an entry. Assigning to a key that already exists replaces its value, because each key appears only once. And the dictionary remembers the order you added the keys.
Ask for a key that isn’t there and Python stops with:
KeyError: 'rating' A list is for “the third one”. A dictionary is for “the one called title”.
4. Read safely with .get(), and loop with .items(). When a key might be missing, use .get() instead of square brackets:
book = {"title": "Moon Garden", "author": "A. Rao", "year": 2019} print(book.get("rating")) print(book.get("rating", "no rating yet")) print("year" in book) for key, value in book.items(): print(key, "->", value) None no rating yet True title -> Moon Garden author -> A. Rao year -> 2019 .get("rating") gives None for a missing key instead of crashing, and .get("rating", "no rating yet") gives the fallback you choose. in checks whether a key exists. .items() hands you each key and value as a pair, so a for loop can unpack both at once.
A simple rule: use square brackets when the key must be there (a crash tells you something is wrong), and .get() when it might be missing.
5. Tuples are fixed, and sets drop duplicates. Two more containers, each with one special trick:
point = (3, 4) x, y = point print(x, y) tags = {"sci-fi", "classic", "sci-fi"} print(len(tags)) print("classic" in tags) print(sorted(set(["b", "a", "b", "c", "a"]))) 3 4 2 True ['a', 'b', 'c'] A tuple uses round brackets. It’s like a list you can’t change after it’s made, which suits values that belong together, such as a point’s x and y. x, y = point unpacks it into two names. Try point[0] = 10 and you get TypeError: 'tuple' object does not support item assignment.
A set uses curly braces without colons. It keeps each value only once, so the repeated “sci-fi” counts once and len(tags) is 2. That makes sets great for “which different values are there?” Sets have no order, so there’s no tags[0]; use sorted() when you want to show them in order. One gotcha: {} on its own makes an empty dictionary, so an empty set is set().
6. A list of dictionaries is the shape of JSON. Put dictionaries inside a list and you can hold a whole table of records:
import json books = [ {"title": "Moon Garden", "year": 2019}, {"title": "Paper Boats", "year": 2023}, ] print(books[1]["title"]) for b in books: print(b["title"], b["year"]) print(json.dumps(books)) Paper Boats Moon Garden 2019 Paper Boats 2023 [{"title": "Moon Garden", "year": 2019}, {"title": "Paper Boats", "year": 2023}] Read books[1]["title"] from left to right: position 1 in the list, then the key “title” in that dictionary. The for loop visits each record in turn.
The last line uses Python’s built-in json module (you’ll learn how import works in issue #20). json.dumps() turns the data into JSON text, the format web APIs, and the AI tools later in this series, send and receive. It looks almost the same as Python. The differences: JSON always uses double quotes, True becomes true, and None becomes null. Tomorrow you’ll read and write JSON files.
Try it (15 minutes)
- In your
sandboxfolder, createplaylist.py, a made-up playlist as a list of dictionaries:
playlist = [ {"title": "Morning Run", "artist": "The Toys", "minutes": 4}, {"title": "Rainy Bus", "artist": "Paper Cups", "minutes": 6}, {"title": "Late Tea", "artist": "The Toys", "minutes": 3}, ] total = 0 titles = [] artists = set() for song in playlist: total = total + song["minutes"] titles.append(song["title"]) artists.add(song["artist"]) print("Songs:", len(playlist)) print("Total minutes:", total) print(sorted(titles)) print(len(artists), "artists") print(playlist[0].get("year", "unknown")) - Run
python3 playlist.py. You should see:
Songs: 3 Total minutes: 13 ['Late Tea', 'Morning Run', 'Rainy Bus'] 2 artists unknown “The Toys” appears twice, but the set keeps it once, so there are 2 artists. None of the songs has a "year" key, so .get() gives the fallback.
- Change the last line to
print(playlist[0]["year"]). Read the last line of the error:KeyError: 'year'. Change it back. - Add
print(playlist[3]). You getIndexError: list index out of range: three songs means indexes 0, 1 and 2. Remove the line. - Find the longest song with a loop. Start with
longest = playlist[0], then for each song, ifsong["minutes"] > longest["minutes"], setlongest = song. Printlongest["title"]. You should getRainy Bus.
Check your understanding: (a) In ["a", "b", "c"], what’s the index of "c"? (b) What’s the difference between book["rating"] and book.get("rating") when there’s no rating? (c) What does sorted() do that .sort() doesn’t?
Answer: (a) 2, because counting starts at 0 (or -1, counting from the end). (b) Square brackets raise KeyError; .get() returns None (or the fallback you give it). (c) sorted() returns a new sorted list and leaves the original alone; .sort() changes the list itself and returns None.
Common beginner mistakes
- Off-by-one: using
len(x)as an index. The last item isx[len(x) - 1], or justx[-1]. - Expecting a slice to include the end:
x[1:3]stops before index 3. -
KeyErrorfrom square brackets on a key that might be missing. Use.get(), or check withinfirst. - Writing
x = x.sort(), which leavesxasNone. - Writing
{}for an empty set. That’s an empty dictionary; useset().
Takeaway
A list keeps items in order and counts from 0. .append(), len(), slicing and sorted() cover most of what you’ll do with one. A dictionary stores values under unique keys: square brackets when the key must exist, .get() when it might not, .items() to loop. Tuples are fixed and sets keep each value once. A list of dictionaries is the shape of JSON.
Where this fits in my roadmap: this is part of my on-ramp, the Python basics everything else sits on. Almost every AI tool I’ll call takes a list of dictionaries in and hands a dictionary back, so reading data[0]["text"] without thinking is a skill worth drilling now. Tomorrow: strings, files and JSON, so that data can come from a file and go back to one.
Over to you: think of something you track, such as books, runs or recipes. Reply with one record as a Python dictionary, like {"title": "Moon Garden", "year": 2019}.
Sources
- Python tutorial, “An Informal Introduction to Python”, Lists (indexing from 0, negative indexes, slicing,
len(),.append()). https://docs.python.org/3/tutorial/introduction.html#lists - Python tutorial, “Data Structures”, More on Lists (
.append(),.sort(), list methods). https://docs.python.org/3/tutorial/datastructures.html#more-on-lists - Python tutorial, “Data Structures”, Tuples and Sequences (tuples, unpacking) and Sets (duplicates removed,
set()for an empty set). https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences - Python tutorial, “Data Structures”, Dictionaries (keys are unique, insertion order,
in,KeyError) and Looping Techniques (.items()). https://docs.python.org/3/tutorial/datastructures.html#dictionaries - Python docs, “Built-in Types”,
dict.get(returns the default, orNone, for a missing key). https://docs.python.org/3/builtins/stdtypes.html#dict.get - Python HOWTO, “Sorting Techniques” (
sorted()returns a new list;list.sort()sorts in place and returnsNone;reverse=True). https://docs.python.org/3/howto/sorting.html - Python docs, “json” (
json.dumps(), and how Python types map to JSON). https://docs.python.org/3/library/json.html - Python docs, “Built-in Exceptions” (
IndexError,KeyError,TypeError). https://docs.python.org/3/builtins/exceptions.html
All examples are made up (the books, songs and artists are invented) and were run with Python 3.13 on Nov 2, 2026. Error messages are from Python 3.13; other versions word some of them differently. Sources checked Nov 2, 2026.
Free: the 5-page roadmap from Novice to AI FDE.
If this was useful, subscribe to The Path to AI FDE for the next issue.








