Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Lists

A list is an ordered collection of values under one name, where each item has a position that starts at 0.

Last updated: 30 Sep, 2026 · Python 3.14

One variable per ticket works for two tickets and fails for two hundred. A list holds any number of them.

Creating, appending and indexing a list · from Tutorial 2 - Python List and Boolean Variables · 8:26 to 13:46
The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
lst = ['Mathematics', 'chemistry', 100, 200, 300, 204]
print(len(lst))

lst.append("Krish")
print(lst)
print(lst[3])
print(lst[6])
print(lst[1:])
print(lst[1:6])

One list can hold text and numbers together. append put the new name at position 6, and the slice lst[1:6] stopped before position 6, so it left that name out. The video names its list lst; below, the list is named after what it holds, a list of tickets.

Syntax:

python
items = [first, second, third]
items[0]           # the first item
items.append(x)    # add x at the end

Creating a list and reading items

Example
tickets = [
    "I was charged twice for one order",
    "My parcel has not arrived",
    "How do I change my password?",
]
print(len(tickets))
print(tickets[0])
print(tickets[-1])

Square brackets make a list, with commas between the items. Writing one item per line is only for reading; Python does not mind either way.

tickets[0] is the first item, because positions count from 0. tickets[-1] counts from the end, so it is always the last item however long the list gets.

Adding items and slicing

Example
tickets.append("The parcel arrived but the box was crushed")
print(len(tickets))
print(tickets[:2])

append adds an item to the end and changes the list itself. tickets[:2] is a slice: a new list of the items from the start up to, but not including, position 2.

Asking for a position that is not there

Example
print(tickets[4])

Four items sit at positions 0 to 3. Asking for position 4 is an IndexError. When a list's length can vary, check len first, or loop over the list instead, which for loops covers.

append vs insert

append(x)insert(i, x)
Where x goesAt the endBefore position i
Exampletickets.append("Hi")tickets.insert(0, "Hi")
Used forCollecting results as they arrivePutting something first

Where lists show up in AI code

  • A batch of texts to send to a model, one after another.
  • The answers that come back, collected with append.
  • The messages of a chat, which Lists of dictionaries builds.
Watch out. Do not name a list list. It works, but it hides Python's own list() function for the rest of the program. Name it after what it holds, such as tickets.
Try it yourself
  • With the three-ticket list, before the append, print tickets[1] and tickets[-2]. Why is it the same ticket?
  • Print tickets[1:].
  • Use tickets.insert(0, "Where is my invoice?") and print the whole list.

You understood something today that you didn't yesterday.