Python Basics for Vibe Coders, Part 5: Lists and Indexes That Start at 0
Create, read, add to, and loop over Python lists. Why indexes start at 0, using -1 for the last item, when IndexError happens, and vibe coding tips.
Ask an AI to "pull just the names out of this Excel file," and you'll often get the result back as a list. A list is a group of values kept in order, and it's one of the most widely used data types in Python. This is Part 5 of the 10-part series "Python Basics for Vibe Coders." It covers how to create a list, read items from it, add to it, and walk through it with a loop, plus why IndexError happens. The examples are written for Python 3.15, and I ran them in Python 3.14.2 to check the results.
Key Takeaways
- A list is an ordered group of values separated by commas inside square brackets
[].- Indexes (position numbers) start at 0.
-1is the last item.- If a list has 3 items, the only valid indexes are 0, 1, and 2.
[3]raises anIndexError.- Use
appendto add to the end,lento count items, andforto take them out one by one.
A list is a row of numbered lockers
Think of a shopping list. It has an order from top to bottom, and you can add or remove items. A list works the same way.
It also helps to picture a row of numbered lockers. There's one catch: the first locker is numbered 0, not 1. That one detail causes a lot of list mistakes.
Create, read, add, and loop
fruits = ["apple", "pear", "grape"]
print(fruits[0], fruits[-1])
fruits.append("orange")
print(len(fruits))
for f in fruits:
print(f)
print(fruits[1:3])
apple grape
4
apple
pear
grape
orange
['pear', 'grape']
| Code | Meaning |
|---|---|
fruits[0] | The first item |
fruits[-1] | The last item (first from the end) |
fruits.append("orange") | Add an item to the end |
len(fruits) | Number of items |
for f in fruits: | Assign each item to f in turn and repeat (Part 4) |
fruits[1:3] | Slice from position 1 up to, but not including, position 3 |
fruits[1:3] is ['pear', 'grape'] for the same reason as range in Part 4: the end number isn't included. Python uses the same "start included, end excluded" rule in many places.
Common trap: is the last item fruits[len(fruits)]?
With 3 items, it seems like fruits[3] should be the last one. It isn't.
fruits = ["apple", "pear", "grape"]
try:
print(fruits[3])
except IndexError as e:
print(e)
list index out of range
(try / except is syntax that catches an error and shows just the message. Part 8 covers it in detail.)
With 3 items, the only indexes are 0, 1, and 2. list index out of range means "there's no locker with that number." To get the last item, use fruits[-1] or fruits[len(fruits) - 1].
Where you'll meet lists in vibe coding
- File lists, a column from a spreadsheet, and search results pulled from the web are often handled as lists.
- Programs show items as 1, 2, 3... on screen, but the code counts from 0. Bugs tend to appear wherever human-facing numbers and list indexes are off by one. The to-do list in Part 10 deals with this gap directly.
- Using
[0]or[-1]on an empty list raises anIndexError. If your code might get zero search results, ask the AI: "Handle what happens when the results are empty."
When you hit an IndexError, print the list's length (len) and the index you were trying to use, right at the line that failed. The cause usually becomes obvious.
Check yourself
- With
nums = [10, 20, 30], what isnums[-1]? → 30. Negative indexes count from the end. - If
len(nums)is 3, what isnums[3]? →IndexError. Index 3 would be the fourth item, and there isn't one.
Try it yourself
Make a list of 5 favorite movies, add one more, then print them all. If 6 movies print, you've got it. If you get stuck, check the parentheses and quotes in append.
movies = ["Parasite", "Oldboy", "Interstellar", "Toy Story", "Inside Out"]
movies.append("La La Land")
print(len(movies))
for m in movies:
print(m)
6
Parasite
Oldboy
Interstellar
Toy Story
Inside Out
La La Land
Previous and next
- Previous: Part 4: for and while Loops and the range Trap
- Next: Part 6: Dictionaries covers dictionaries, which look up values by name instead of by number, and the
KeyErroryou'll often see in AI code.
The full series list is at the end of Part 10.