How to Use Python Lambda With the Sort Method

preview_player
Показать описание
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.

Рекомендации по теме
Комментарии
Автор

Hey, been following you for a while a love your content. I hope you won't mind this seeing as my channel is not-for-profit and a non-monetised (no-ads, no affiliate links). I make tutorials for intermediate Python learners and those who want to increase their experience and exposure to the language through specific examples covering specific built-in modules and other core language features. Hope there are some other commenters who would benefit from and see this. The most popular ones at the moment are super() in 5 mins, creating a RESTful server in 3 minutes with no third party libraries and metaclass usage.
Keep making the great content!

LivePython
Автор

This is one way to explain lambda functions, yes, but it’s not the most pythonic code in the world. I wish you would at least mention itemgetter and attrgetter from the standard library, which is what I would use in production.

traal
Автор

Thanks mate, especially for the way how to sort a list of objects.

szymonf
Автор

Really clear and simple explanations! Is there a way to define how a class should be sorted within the class? So that every time you wanted to compare objects you wouldn't need a lambda function?

TheSlyProductions
Автор

So easy to learn, thank you very much.

aspirin
Автор

Do you want to master Machine Learning ? So do I

spacenodus
Автор

wow you overcomplicate things for no reason, like, why using lower() for no reason at the names sorting as a first example?

caioandrade