Day 18 · ~14m

Lists

Creating lists, indexing, slicing, and len() — your first real collection.

🧑‍💻

OK I think I get this — a list is basically a column in my spreadsheet, right? Like a column of customer names?

👩‍🏫

That's a really solid mental model. A list is an ordered, mutable collection of items. "Ordered" means items stay in the order you put them — just like rows in a column. "Mutable" means you can change it after creating it — add items, remove items, rearrange them. Unlike a spreadsheet column, though, a list can hold any mix of types:

customers = ["Alice", "Bob", "Charlie"]
scores = [10, 20, 30, 40, 50]
mixed = ["Alice", 92, True, 3.14]
empty = []

And len() tells you how many items are inside — like COUNTA() in your spreadsheet:

len(customers)    # 3
len(empty)        # 0

Remember last week when you had separate variables for everything? customer1 = "Alice", customer2 = "Bob", customer3 = "Charlie"? That gets ridiculous at scale. A list is the fix. One name, any number of items.

🧑‍💻

I have 500 customer records at work. Could I put all of them in a list?

👩‍🏫

500? Python won't even blink. This works for 3 items or 3 million. That's the whole point of collections — you write code once, and it handles whatever volume you throw at it.

But first you need to get items out of the list. That's indexing — square brackets and a position number. Python counts from zero:

customers = ["Alice", "Bob", "Charlie", "Diana", "Eli"]
customers[0]    # "Alice"   — first item
customers[1]    # "Bob"     — second item
customers[4]    # "Eli"     — fifth (last) item

If you try to access an index that doesn't exist, Python raises an IndexError:

customers[10]   # IndexError: list index out of range

This is one of the most common errors beginners hit. Always check len() if you're unsure.

🧑‍💻

What if I want the last customer but I don't know how long the list is? My customer list changes every day.

👩‍🏫

Negative indexing. Python lets you count backwards from the end:

customers[-1]    # "Eli"      — last item
customers[-2]    # "Diana"    — second to last
customers[-3]    # "Charlie"  — third to last

Think of -1 as a shortcut for len(customers) - 1. It's cleaner and less error-prone. You'll use [-1] all the time — "give me the most recent entry" is one of the most common operations in data work.

🧑‍💻

Can I grab multiple items at once? Like, I need the first three customers for a report.

👩‍🏫

That's slicing. The syntax is list[start:stop] where start is included and stop is excluded:

customers = ["Alice", "Bob", "Charlie", "Diana", "Eli"]
customers[1:4]    # ["Bob", "Charlie", "Diana"]
customers[:3]     # ["Alice", "Bob", "Charlie"] — first three
customers[2:]     # ["Charlie", "Diana", "Eli"] — from third to end
customers[:]      # full copy of the list

The "stop is excluded" rule trips people up at first, but it has a nice property: customers[:3] gives you exactly 3 items. The length of a slice is always stop - start.

🧑‍💻

What about every other item? Like, I sometimes need to look at every other row in my spreadsheet.

👩‍🏫

Add a third number: list[start:stop:step]:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[::2]     # [0, 2, 4, 6, 8] — every other item
numbers[1::2]    # [1, 3, 5, 7, 9] — every other, starting at 1
numbers[::-1]    # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] — reversed!

The [::-1] trick is the classic Python way to reverse a list. You'll see it everywhere.

🧑‍💻

Slicing never crashes, even if I go past the end?

👩‍🏫

Correct — slicing is forgiving. If your indices go beyond the list, Python just gives you what it can:

customers[1:100]   # ["Bob", "Charlie", "Diana", "Eli"]
customers[10:20]   # [] — empty list, no error

This is different from single-item indexing, which does raise an IndexError. Slicing always returns a list, even if it's empty. Think of it as a safe "give me what you can" operation.

Let's practice working with indices and slices.

Practice your skills

Sign up to write and run code in this lesson.

Already have an account? Sign in