Step: Sorting: sorted() with a key

Sorting: sorted() with a key

sorted(iterable, key=fn) sorts using the value fn returns for each item, instead of comparing the items directly — a lambda is a quick one-off function for this.

Why it matters: you almost never sort a list of dicts/objects directly; key= is how you tell Python what to sort by.

Example: sorted([3, 1, 2]) is [1, 2, 3]; sorted(words, key=len) sorts by length.

Your turn: sort people by age (ascending) and print just their names, using key=lambda p: p["age"].

Pitfall: sorted() returns a new list; the original people list is untouched (unlike list.sort(), which sorts in place and returns None).

Setup:
people = [{"name": "Ann", "age": 34}, {"name": "Sam", "age": 21}, {"name": "Bo", "age": 45}]
Your code:
Expected output:
['Sam', 'Ann', 'Bo']
Step 14 of 18