Day 17 · ~14m

Python List Methods: sort, append, and Slices

Learn to sort lists with sorted() and key=lambda, then slice them to get top N results. Build real data-filtering skills beyond simple iteration.

student (excited)

Wait, we finished filtering the sales with collect_valid_sales() yesterday. So now I have a clean list of tuples — just the real sales. But Diane just asked me: "Can you show me the top 5 sales by amount?" And I realized... I don't know how to do that without sorting.

teacher (focused)

That's the exact problem sorted() solves. You have a list, you need it rearranged by some criteria, and then you want to pick the best ones. Right now, if you tried a regular for loop to find the max, you'd loop through the whole list repeatedly. sorted() is smarter — it rearranges the entire list in one pass.

Here's the core: sorted() takes a list and returns a new list, sorted in ascending order by default:

sales = [
    ('Alice Chen', 6800.00),
    ('Bob Kumar', 340.50),
    ('Carol Santos', 1250.00),
]
sorted_sales = sorted(sales)
print(sorted_sales)
# Output: [('Alice Chen', 6800.00), ('Bob Kumar', 340.50), ('Carol Santos', 1250.00)]

Wait, what? It sorted by the name, not the amount. That's because when you sort tuples, Python compares the first element by default. The names come first, so it alphabetizes them. But you want to sort by amount, which is at index 1.

student (thinking)

So I need to tell sorted() "ignore the first part, look at the second part of each tuple." But how?

teacher (encouraging)

With a key function. The key is a function that tells sorted() which part of each item to actually compare. Here's where lambda comes in:

sorted_sales = sorted(sales, key=lambda sale: sale[1])
print(sorted_sales)
# Output: [('Bob Kumar', 340.50), ('Carol Santos', 1250.00), ('Alice Chen', 6800.00)]

Read lambda sale: sale[1] as: "For each item in the list (I'm calling it sale), extract sale[1] — the amount — and compare by that." sorted() uses those amounts to decide the order. Now the smallest amount is first.

But you want the biggest amount first, right? That's where reverse=True comes in:

sorted_sales = sorted(sales, key=lambda sale: sale[1], reverse=True)
print(sorted_sales)
# Output: [('Alice Chen', 6800.00), ('Carol Santos', 1250.00), ('Bob Kumar', 340.50)]

Now the list is sorted by amount, descending. Alice's big sale is first.

student (confused)

Hold on... lambda sale: sale[1] is a function? Like, a full function? With def?

teacher (serious)

lambda is a shorthand for tiny, anonymous functions. Instead of writing:

def get_amount(sale):
    return sale[1]

sorted_sales = sorted(sales, key=get_amount)

You write:

sorted_sales = sorted(sales, key=lambda sale: sale[1])

Both work identically. lambda is just "define a quick function right here." It takes input (the thing before the colon — sale), does something (the thing after the colon — return sale[1] implicitly), and that's it. No def, no return keyword — just the expression.

student (amused)

So it's Python's way of saying "I need a function but I'm too lazy to name it properly."

teacher (amused)

Exactly. And once the sorted() call finishes, the lambda is gone. You never use it again. It's a disposable function, made exactly when you need it.

Now, once you have a sorted list, the last piece is slicing. You want the top 3, not all of them:

top_3 = sorted_sales[:3]
print(top_3)
# Output: [('Alice Chen', 6800.00), ('Carol Santos', 1250.00), ('Bob Kumar', 340.50)]

Remember slicing from earlier? [:3] means "start from the beginning, go up to (but not including) index 3." So you get indices 0, 1, and 2. That's the top 3.

Put it together:

def get_top_sales(sales, n):
    sorted_sales = sorted(sales, key=lambda sale: sale[1], reverse=True)
    return sorted_sales[:n]

Pass in a list of tuples and the number you want, and you get back the top N by amount.

student (focused)

What if n is bigger than the list? Like, I ask for the top 100 sales but there are only 5?

teacher (encouraging)

Slicing is forgiving. If you ask for [:100] but the list has only 5 items, you just get all 5. Python doesn't crash — it gives you what exists. It's safe.

student (curious)

And what if I wanted to sort by name instead? Or by some other field later when we have dictionaries?

teacher (proud)

You'd just change the lambda. key=lambda sale: sale[0] for name. Or key=lambda person: person['age'] for dictionaries. The pattern is the same. But here's the thing: we're still dealing with tuples — a name and an amount glued together. Real sales data has way more information: region, date, product, commission rate. A tuple gets messy fast. Tomorrow we're moving to dictionaries — key-value pairs. One sale can have twenty fields, and you pick exactly which ones matter for sorting. That's where data gets real.