Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
The key argument
The itemgetter() function
11. Summary


Good job! It's time to wrap things up! Let's review what we've learned:

  1. Functions like sort(), sorted(), min() and max() can take an optional key parameter that specifies a custom sort order:
    sorted(products, key=str.lower)
  2. Sorting in Python is stable. When multiple items have a key of the same order, their original order will be preserved.
  3. The key argument can accept lambdas (i.e., anonymous functions):
    products.sort(key=lambda product: product.replace(' ',''))
  4. To sort a list of tuples by a given element, use:
    products.sort(key=lambda product: product[2])
  5. You can also pick tuple elements to sort using itemgetter():
    products.sort(key=itemgetter(0, 2))

How about a short quiz now?


Click Next exercise to continue.