Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Sort a List of Numbers in Python

Learn how to sort a list of numbers in Python using various methods, including built-in functions like sort() and sorted(), as well as custom sorting algorithms. …


Updated May 26, 2023

Learn how to sort a list of numbers in Python using various methods, including built-in functions like sort() and sorted(), as well as custom sorting algorithms.

What is Sorting in Python?

Sorting in Python refers to the process of arranging a list of elements (in this case, numbers) in a specific order, such as ascending or descending. This is an essential operation that has numerous applications in data analysis, scientific computing, and more.

Step 1: Understanding Lists in Python

Before diving into sorting, it’s crucial to understand how lists work in Python. A list is a collection of elements (numbers, strings, etc.) that can be accessed using indices. You can create a list by enclosing elements within square brackets [].

Example: Creating a List

numbers = [4, 2, 9, 6, 5]

Step 2: Sorting Lists in Python

There are several ways to sort lists in Python:

Method 1: Using the sort() Function

The sort() function sorts the list in-place (i.e., it modifies the original list). This means that once you call sort(), the original list will be rearranged according to the sorting criteria.

Example: Sorting a List using sort()

numbers = [4, 2, 9, 6, 5]
numbers.sort()
print(numbers)  # Output: [2, 4, 5, 6, 9]

Method 2: Using the sorted() Function

The sorted() function returns a new sorted list without modifying the original list. This is useful when you need to preserve the original order.

Example: Sorting a List using sorted()

numbers = [4, 2, 9, 6, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [2, 4, 5, 6, 9]

Method 3: Custom Sorting Algorithms

While sort() and sorted() are efficient for many use cases, there might be situations where you need to implement custom sorting algorithms. Python provides several built-in functions (e.g., heapq, bisect) that can help with this.

For example, if you want to sort a list of numbers based on their frequency in a given data set:

Example: Custom Sorting using Dictionary

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
frequency_dict = {}
for num in data:
    if num not in frequency_dict:
        frequency_dict[num] = 0
    frequency_dict[num] += 1

sorted_data = sorted(data, key=lambda x: (frequency_dict[x], -data.count(x)))
print(sorted_data)  # Output: [4, 4, 4, 4, 3, 3, 3, 2, 2, 1]

Conclusion

Sorting lists of numbers in Python is a fundamental operation that can be performed using various methods. This guide demonstrated how to sort lists using built-in functions like sort() and sorted(), as well as custom sorting algorithms.

Whether you’re a beginner or an advanced user, mastering sorting techniques will help you tackle a wide range of problems in data analysis, scientific computing, and more.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp