Day 14 · ~14m

Python Lists: Store Multiple Values in One Variable

Lists let you store multiple values in a single container. Learn [], .append(), and len() to build and manipulate collections of data dynamically.

student (thinking)

You can validate a sale. You can process a sale. You can loop through a bunch of sales. But right now I'm still working with one sale at a time. What if I need to keep the good ones and throw away the bad ones?

teacher (encouraging)

That's exactly the question that leads to lists. Right now your programs are like a calculator — you put in data, compute something, print the result, and the result vanishes. But real code needs to collect things. A spreadsheet isn't one row; it's thousands of rows that stay in memory until you save the file. A list in Python is your answer.

student (curious)

So a list is like... a container?

teacher (focused)

Exactly. A container that holds multiple values. You create one with square brackets, and you add values with .append(). Here's the pattern:

# Create an empty list
valid_sales = []

# Add items one by one
valid_sales.append(('Alice Chen', 1250.50))
valid_sales.append(('David Park', 890.00))

print(valid_sales)          # [('Alice Chen', 1250.5), ('David Park', 890.0)]
print(len(valid_sales))     # 2

You've seen lists before — you iterated over them with for loops. But now you're building them from scratch. .append() adds a single item to the end of the list. len() tells you how many items are in the list.

student (excited)

So if I loop through a bunch of sales and only add the valid ones to a list, I end up with just the good data?

teacher (proud)

That's it. Watch what happens when you combine validation — remember is_valid_sale from yesterday — with list building:

def is_valid_sale(name, amount):
    return len(name.strip()) > 0 and amount > 0

raw_sales = [
    ('Alice Chen', '1250.50'),
    ('', '340.50'),
    ('Carol Santos', '-50.00'),
    ('David Park', '890.00'),
]

valid = []
for name, amount_str in raw_sales:
    amount = float(amount_str)
    if is_valid_sale(name, amount):
        valid.append((name, amount))

print(valid)
# [('Alice Chen', 1250.5), ('David Park', 890.0)]
print(f"Valid sales: {len(valid)} out of {len(raw_sales)}")
# Valid sales: 2 out of 4

You've filtered the data. You started with four sales, validated each one, and kept only the ones that passed. The empty name and negative amount got rejected. Now your list contains only the data you trust.

student (amused)

This is basically what I wanted to do last week when I had those hardcoded tuples. But now I can build the list dynamically. And I can use it later — like calculate the total of all valid sales?

teacher (encouraging)

Exactly. Once you have a list of valid sales, you can loop through it again to calculate a running total, find the highest amount, count how many came from each region — whatever you need. Lists are the fundamental way Python programs collect data.

student (focused)

OK so the function takes a list of tuples with string amounts, and returns a list of tuples with float amounts, right?

teacher (focused)

Yes. The input is raw_sales — tuples where the amount is a string. You convert it to float, validate it, and build a new list with the valid ones. Notice that the amount in the output list is a float, not a string. You've cleaned and transformed the data.

student (proud)

This feels powerful. I'm filtering data, converting types, and building something I can use later. And next week we add more list powers — sorting, slicing, all that?

teacher (proud)

Next week you're going to discover that lists have way more methods — .sort(), .reverse(), .index(). And we're adding dictionaries, which are like lists but with named slots instead of numbered positions. You're building the data structures that every real program relies on. This is the foundation.