How to Sort a List in Python (Beginner Guide)
How to Sort a List in Python: A Complete Beginner’s Guide
Introduction
If you are just starting out with Python, one of the most useful skills you can learn early on is how to sort a list in Python. Whether you are organizing a list of names, ranking scores in a game, or simply cleaning up data, sorting is something you will do constantly as a developer. The good news is that Python makes sorting incredibly easy. You do not need to write complicated algorithms from scratch. Python gives you two powerful built-in tools — the sort() method and the sorted() function — and once you understand how they work, you will be sorting lists like a pro in no time. In this guide, we will walk through everything a beginner needs to know, including how to sort in ascending and descending order, how to sort lists of strings, and even how to do custom sorting. Let’s dive in.
Understanding the Two Main Ways to Sort a List in Python
Python gives you two primary ways to sort a list, and it is important to understand the difference between them before you start writing code. The first is the sort() method, which is called directly on a list and changes the original list permanently. This is known as sorting “in place.” The second is the sorted() function, which takes any list as input and returns a brand new sorted list, leaving the original list completely unchanged. Here is a simple example of each. Say you have a list of numbers: numbers = [5, 2, 9, 1, 7]. If you call numbers.sort(), the list itself becomes [1, 2, 5, 7, 9]. But if you use sorted(numbers) instead, Python returns a new list [1, 2, 5, 7, 9] while the original numbers list stays as [5, 2, 9, 1, 7]. Choosing between these two depends on whether you need to keep the original data. If you might need the unsorted version later, go with sorted(). If you are done with the original order and just want everything sorted, sort() is perfectly fine and slightly more memory efficient.
Sorting in Ascending and Descending Order
By default, both sort() and sorted() arrange items from smallest to largest — this is called ascending order. For numbers, that means from the lowest value to the highest. For strings, that means alphabetically from A to Z. But what if you want the opposite? Both tools accept an optional parameter called reverse that you can set to True to sort in descending order. For example, if you have scores = [88, 72, 95, 60, 100] and you want the highest scores first, you would write scores.sort(reverse=True), which gives you [100, 95, 88, 72, 60]. The same works with strings. If you have names = ["Charlie", "Alice", "Bob"] and you call sorted(names), you get ["Alice", "Bob", "Charlie"]. Call sorted(names, reverse=True) and you get ["Charlie", "Bob", "Alice"]. One important thing to remember for beginners: Python is case-sensitive when sorting strings. Uppercase letters are sorted before lowercase letters by default, which can sometimes produce surprising results. For example, a list like ["banana", "Apple", "cherry"] sorted normally would give ["Apple", "banana", "cherry"] because uppercase A comes before lowercase b in Python’s default sorting rules. To avoid this, you can use a key function, which brings us to our next section.
Using the Key Parameter for Custom Sorting
One of the most powerful features of Python’s sorting tools is the key parameter. This lets you tell Python exactly how to evaluate each item when deciding the sort order. Think of it like giving Python a rule to follow. For example, if you want to sort a list of strings without caring about uppercase or lowercase letters, you can pass str.lower as the key: sorted(names, key=str.lower). This tells Python to treat all letters as lowercase when comparing them, so “Apple” and “apple” would be treated the same way. The key parameter becomes even more useful when you are sorting lists of more complex items, like dictionaries. Imagine you have a list of student dictionaries: students = [{"name": "Alice", "grade": 90}, {"name": "Bob", "grade": 75}, {"name": "Charlie", "grade": 85}]. To sort this list by grade, you would write sorted(students, key=lambda s: s["grade"]). The lambda here is just a small, anonymous function that tells Python to look at the “grade” value of each dictionary when sorting. You can also sort by string length by writing sorted(words, key=len), where len is Python’s built-in length function. The key parameter is incredibly flexible and is something you will use again and again as your Python skills grow. Do not worry if lambdas feel confusing at first — they become very natural with a little practice.
Frequently Asked Questions
What is the difference between sort() and sorted() in Python?
The main difference is that sort() is a method that modifies the original list directly and returns None, while sorted() is a built-in function that returns a new sorted list and leaves the original list untouched. Use sort() when you want to change the list in place and do not need the original order anymore. Use sorted() when you want to keep the original list as it is and work with a separate sorted copy. Another practical difference is that sorted() can work on any iterable, not just lists — for example, you can pass it a tuple or a string — while sort() is only available on list objects.
Can you sort a list of mixed data types in Python?
In Python 3, you generally cannot sort a list that contains mixed data types, such as a list with both integers and strings like [1, "apple", 3]. If you try, Python will raise a TypeError because it does not know how to compare a number to a string. In Python 2, this was allowed because the language had default comparison rules for different types, but Python 3 removed this behavior to make code more predictable and less error-prone. If you find yourself with a mixed-type list, you should clean or convert your data first before attempting to sort it. For example, you could convert all items to strings using sorted(mixed_list, key=str) so that Python has a consistent way to compare each item.
How do you sort a list in Python without changing the original?
The easiest way to sort a list in Python without changing the original is to use the sorted() function instead of the sort() method. For example, if you have my_list = [3, 1, 4, 1, 5], you can write new_list = sorted(my_list), and my_list will remain [3, 1, 4, 1, 5] while new_list will be [1, 1, 3, 4, 5]. Another option is to create a copy of the list first and then sort the copy using sort(). You can do this by writing copy_list = my_list.copy() followed by copy_list.sort(). Both approaches work well, but using sorted() is cleaner and requires fewer lines of code.
Conclusion
Learning how to sort a list in Python is one of those foundational skills that will make you a much more effective programmer right from the start. You now know how to use the sort() method to sort a list in place, how to use the sorted() function to get a new sorted list without touching the original, how to sort in both ascending and descending order using the reverse parameter, and how to use the key parameter for custom and case-insensitive sorting. These tools are simple, readable, and incredibly powerful. As you continue building your Python skills, you will find yourself reaching for these sorting techniques constantly — from beginner projects like sorting to-do lists to more advanced work like ranking data in data science and web development. Practice these examples in your own Python environment, experiment with different lists, and do not be afraid to make mistakes. That is how real learning happens. Happy coding!