Instruction
Good job! It's time to wrap things up! Let's review what we've learned:
- Functions like
sort(),sorted(),min()andmax()can take an optionalkeyparameter that specifies a custom sort order:sorted(products, key=str.lower)
- Sorting in Python is stable. When multiple items have a
keyof the same order, their original order will be preserved. - The
keyargument can acceptlambdas (i.e., anonymous functions):products.sort(key=lambda product: product.replace(' ','')) - To sort a list of tuples by a given element, use:
products.sort(key=lambda product: product[2])
- You can also pick tuple elements to sort using
itemgetter():products.sort(key=itemgetter(0, 2))
How about a short quiz now?
Exercise
Click to continue.



