Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

How to Use Lambda With the Sort Method

Give Feedback

In this lesson, you’ll see how to use a lambda for the key of the sort() method of lists. sort() has two parameters. key is None by default. reverse is False by default.

You won’t be altering reverse, but you will be looking at key because you can use a lambda expression to alter the behavior of sort(). When you use lambda, you can extend key to be much more versatile.

Comments & Discussion

Justin Richie on July 21, 2020

At 1:25 in this video, what is the [-1] doing here?

Darren Jones RP Team on July 21, 2020

The [-1] gives the last element of a list (in this case the surnames as the names were split with .split(), so that the last element of that list would be the surname). You could use [1], but that would only work if each name given had only two elements - firstname, lastname. if anyone had a middle name, then it wouldn’t work correctly.

Become a Member to join the conversation.