← Back to Chapters

Python Sort Lists

? Python Sort Lists

⚡ Quick Overview

In Python, lists are mutable sequences that can be sorted in-place or used to create new sorted lists. You can sort strings, numbers, and even complex objects using built-in tools like list.sort() and sorted(), along with powerful options such as reverse and key.

? Key Concepts

  • list.sort() sorts a list in-place (modifies the original list).
  • sorted() returns a new sorted list, leaving the original unchanged.
  • Sorting is ascending by default (A–Z / smallest to largest).
  • Use reverse=True to sort in descending order.
  • Use the key parameter to define custom sorting logic.

? Syntax & Theory

⬆️ Ascending Order

The simplest way to sort a list is to call sort() on it. This changes the list itself and arranges the items in ascending order.

my_list.sort()

⬇️ Descending Order

To sort the list in descending order, pass reverse=True to sort() or sorted().

my_list.sort(reverse=True)

? Sorting Numbers

Numeric lists are sorted from the smallest to the largest value by default.

⚙️ Custom Sorting with key

The key parameter takes a function that returns a value to sort by. This allows advanced, custom sorting rules.

? sort() vs sorted()

  • list.sort() modifies the original list and returns None.
  • sorted(iterable) returns a new list and does not change the original.

? Code Examples

⬆️ Basic Sorting (Ascending)

You can sort a list of strings alphabetically using sort().

? View Code Example
fruits = ["orange", "apple", "banana", "cherry"]
fruits.sort()
print(fruits)

⬇️ Descending Order

Use the reverse=True argument to sort in descending order.

? View Code Example
fruits = ["orange", "apple", "banana", "cherry"]
fruits.sort(reverse=True)
print(fruits)

? Sorting Numbers

Numeric lists can be sorted just as easily, from smallest to largest.

? View Code Example
numbers = [100, 50, 65, 82, 23]
numbers.sort()
print(numbers)

⚙️ Custom Sorting with key

Use a key function to define a custom rule. In this example, numbers are sorted by how close they are to 50.

? View Code Example
numbers = [100, 50, 65, 82, 23]
numbers.sort(key=lambda x: abs(x - 50))
print(numbers)

? sort() vs sorted()

sorted() creates a new sorted list, without changing the original.

? View Code Example
numbers = [3, 1, 4, 2]
print(sorted(numbers))   # new sorted list
print(numbers)           # original list unchanged

? Live Output & Explanation

? Understanding the Results

  • For the fruits example with fruits.sort(), the output will be: ['apple', 'banana', 'cherry', 'orange'] (alphabetical order).
  • With fruits.sort(reverse=True), the output becomes: ['orange', 'cherry', 'banana', 'apple'] (reverse alphabetical).
  • For numbers = [100, 50, 65, 82, 23], after numbers.sort() you get: [23, 50, 65, 82, 100].
  • In the custom sort example with key=lambda x: abs(x - 50), the list is ordered by distance from 50 (closest to farthest).
  • With sorted(numbers), you see a sorted copy, while numbers itself remains unchanged.

? Tips & Best Practices

  • Use sort() when you don’t need the original order of the list.
  • Use sorted() when you want to keep the original list unchanged.
  • Prefer simple, readable key functions; complex logic can reduce clarity.
  • Make sure the elements in the list are of comparable types (e.g., all strings or all numbers).

? Try It Yourself

  • Create a list of words and sort them by length using the key=len parameter.
  • Sort a list of numbers in descending order using reverse=True.
  • Use sorted() to create a new sorted list while keeping the original intact, then print both lists.
  • Write a custom sort that orders numbers by their remainder when divided by 3 (use key=lambda x: x % 3).