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.
How to Use sorted() and .sort() in Python: Summary
All right! Now you should have a pretty good idea of the differences between
.sort() in Python. Let’s quickly recap on what you learned. First, you used the built-in function
sorted() to sort numbers and strings.
You then learned how to pass in
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.
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
.sort() in your next project.
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.
Become a Member to join the conversation.