Good job! It's time to wrap things up! Let's review what we've learned:
- Functions like
max() can take an optional
key parameter that specifies a custom sort order:
- Sorting in Python is stable. When multiple items have a
key of the same order, their original order will be preserved.
key argument can accept
lambdas (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)
- You can also pick tuple elements to sort using
How about a short quiz now?