Lists
A list is an ordered collection of values in square brackets, separated by commas, that you can change later.
Used in 126 lessons of the course, from 00.02.
You write a list with square brackets: ["a", "b", "c"]. It keeps values in order, and each one has a position that starts at 0. It can mix types and it can be empty: [].
You read it left to right, like a shopping list. It is mutable: you can add, remove or replace items without building a new list.
For a long list, you can put one item per line. Python allows a trailing comma, which makes it easier to add or remove lines.
In the course you see this from lesson 00.02, with lines like PACKAGES = [, which opens a list of names to loop over later.
PACKAGES = [
"langchain",
"pydantic",
]
Reach for it when: you need to keep several values in order, like names, messages or results, and maybe change or loop over them.
Examples
Common mistake
names = ["ana", "luis"] print(names[2])
Why: With two items, the valid positions are 0 and 1. Position 2 does not exist.
How to write it: Use a position inside the range, or names[-1] for the last item.
Common mistake
a = [1, 2] b = a b.append(3) print(a)
Why: `b = a` does not copy the list: both names point to the same one. Changing one changes the other.
How to write it: Make a copy with b = a.copy() or b = list(a).
