How to Use sorted() and .sort() in Python: Overview
All programmers will have to write code to sort items or data at some point. Sorting can be critical to the user experience in your application, whether it’s ordering a user’s most recent activity by timestamp, or putting a list of email recipients in alphabetical order by last name. Python sorting functionality offers robust features to do basic sorting or customize ordering at a granular level.
In this course, you’ll learn how to sort various types of data in different data structures, customize the order, and work with two different methods of sorting in Python.
By the end of this tutorial, you’ll know how to:
- Implement basic Python sorting and ordering on data structures
- Differentiate between
sorted()
and.sort()
- Customize a complex sort order in your code based on unique requirements
For this course, you’ll need a basic understanding of lists and tuples as well as sets. Those data structures will be used in this course, and some basic operations will be performed on them. Also, this course uses Python 3, so example output might vary slightly from what you’d see if you were using Python 2.
00:00
Hello! And welcome to the Real Python guide to using the sorted()
function and .sort()
method in Python. Sorting is a critical task in computer science and is often a first step in organizing some type of data structure. In this set of videos, you’re going to learn how to use the built-in function sorted()
for ordering numbers and strings, how to use the reverse
and key
arguments, and then how to do the same thing using the .sort()
method. We’ll then talk about some common issues with sorting and steps to avoid these problems. Finally, you’ll learn some key differences between sorted()
and .sort()
and when to use one or the other. After this course, you should feel comfortable customizing Python sort tools to your own project, which can save you a lot of time when you need something in a specific order.
Become a Member to join the conversation.