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.
print(). In a .py file and in the editor here, wrap it in print(), as the run below does.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])6 ['Mathematics', 'chemistry', 100, 200, 300, 204, 'Krish'] 200 Krish ['chemistry', 100, 200, 300, 204, 'Krish'] ['chemistry', 100, 200, 300, 204]
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:
items = [first, second, third]
items[0] # the first item
items.append(x) # add x at the endCreating a list and reading items
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])3 I was charged twice for one order How do I change my password?
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
tickets.append("The parcel arrived but the box was crushed")
print(len(tickets))
print(tickets[:2])4 ['I was charged twice for one order', 'My parcel has not arrived']
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
print(tickets[4])Traceback (most recent call last):
File "main.py", line 1, in <module>
print(tickets[4])
IndexError: list index out of rangeFour 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 goes | At the end | Before position i |
| Example | tickets.append("Hi") | tickets.insert(0, "Hi") |
| Used for | Collecting results as they arrive | Putting 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.
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.Related
- Previous: if, elif and else
- Next: for loops
- Reference: More on Lists in the Python tutorial
- With the three-ticket list, before the append, print
tickets[1]andtickets[-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.