Sorting8:08 with Kenneth Love
Sorting lists is often required to solve a particular problem, but that changes the list! What can we do?
sorted() takes an iterable to sort and returns a new list from it. If you need to customize the sorting, pass a function in as the
key argument. There's an optional
reverse argument that will cause the results to be reversed before they're returned.
operator.itemgetter()gets items from an object that supports that operation. We use it to get keys from dicts but it has other uses too.
operator.attrgetter() gets attributes from an object.
Wait, you didn't talk about
reversed in the video!
reversed() is important but isn't all the unique or remarkable for a video right next to
sorted(reverse=True) so I left it out. But good job, you, finding it here!
reversed() takes an iterable and reverses it, returning a new iterable. This new iterable has to be turned into a list/tuple/etc to get items from it by index.
You need to sign up for Treehouse in order to download course files.Sign up