Three ways to sort a sequence

Python offers us three common approaches to the general problem of sorting a list of complex items.

  • We can sort with the sorted() generator function. This will duplicate an object as part of sorting.
  • We can sort a list with its sort() method and a key function. This will mutate the list into the requested order.
  • We can create an intermediate sequence of objects which can be sorted easily. This is sometimes called the wrap-sort-unwrap design pattern.

In order to look at each of these in some detail, we need a collection of complex objects which we can sort. We'll use a simple dataset based on a case study in the NIST Engineering Statistics Handbook, section 7.1.6. See http://www.itl.nist.gov/div898/handbook for more information. ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.