How to Use sorted() and .sort() in Python: Summary
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
All right! Now you should have a pretty good idea of the differences between sorted()
and .sort()
in Python. Let’s quickly recap on what you learned. First, you used the built-in function sorted()
to sort numbers and strings.
00:14
You then learned how to pass in reverse
and key
arguments to customize the sorting behavior. In here, we talked about lambda
functions a little bit, and if you plan on using key
arguments, I highly recommend getting comfortable with them.
00:27
After that, we went to the .sort()
method and you learned how that is used to sort lists in place, and also some of the differences between how it doesn’t return values in how it outputs data.
00:40
You then worked through a couple of examples to show some common errors and gotchas while sorting, including things like making sure that your elements are comparable and how characters are sorted based on their Unicode values. Lastly, you worked through an example which showed where sorted()
would have been better used than .sort()
. And with that, you’re all set to go use sorted()
and .sort()
in your next project.
01:04
By defining your own functions and lambda
expressions, you can really customize these for your specific use case. So the next time you feel like you need to define a new sorting algorithm for your project, see if you can use one of these instead. Thanks for watching.
Faheem Faheem on Jan. 1, 2020
Good review of sorted() and .sort(). Thank you!
Harsha Vardhan on Jan. 2, 2020
Now, I learned a better way to sort mixed numbers. Thank you Joe..
Venkat Ganti on Jan. 2, 2020
Thank you Joe, it is good tutorial. My take away is usage of lambda function and when to use sort and sorted.
Kevin Freeman on Jan. 3, 2020
Very clear explanations and realistic examples. Thanks Joe!
Sandhya on Jan. 4, 2020
Thank you. Joe.
rallegre on Jan. 6, 2020
Great job Joe! Tutorial’s like this is the reason why subscribed to Real Python
sion on Jan. 6, 2020
Useful stuff. I liked the way you presented it. Thank you Joe.
Billiam on Jan. 7, 2020
Very nicely presented. Easy to understand and follow. Thanks Joe.
RetiringInComo on Jan. 15, 2020
Totally agree, thought it was going to be boring but had several ah - ha moments. Thanks Joe!!
majid021 on Jan. 30, 2020
Thank you for explaining the difference between sort and sorted in detail with good examples. I was using both sort and sorted thinking that they don’t have much difference, but after watching i got really good idea when to use what.
Thanks Joe.
Valeriy Pomulev on July 2, 2020
Thanks a lot, Joe. Great training.
ingdanielguerrero on July 17, 2020
great tutorial, thank you.
davedanger on Oct. 18, 2020
I’m taking an MIT EDX course (6.002.X) but Real Python is my goto for detailed explanation when and where needed. This helped me “sort” out some of the nuances that they glossed over in the lecture material. Thanks! Your tutorial is very well presented, and the key = lambda x:… explanation was what I needed for a current problem set.
Become a Member to join the conversation.
Pakorn on Dec. 31, 2019
Great tutorial, very useful, esp. the common issues section.