Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up