How to Sort a List in Python in Ascending and Descending Order

This code demonstrates three ways to sort a list in Python: returning a new sorted list with sorted(), reversing the sort order, and sorting a list in place with the list.sort() method.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 12 views 0 copies

Python code

31 lines
Python 3.9+
def get_sorted_data(numbers):
    """Return a new list sorted in ascending order."""
    return sorted(numbers)


def reverse_sort(data):
    """Return a new list sorted in descending order."""
    return sorted(data, reverse=True)


def sort_in_place(data):
    """Sort the given list in place (modifies original)."""
    data.sort()
    return data


if __name__ == "__main__":
    nums = [42, 17, 8, 99, 23, 5]
    print("Original list:", nums)
    print("Ascending:", get_sorted_data(nums))
    print("Descending:", reverse_sort(nums))
    print("Original after sorted() still unchanged:", nums)

    new_nums = [10, 3, 8, 1]
    sort_in_place(new_nums)
    print("In-place sorted:", new_nums)  # original is now changed

    # Sorting a list of strings
    words = ["apple", "Banana", "cherry", "date"]
    print("Strings sorted (case-sensitive):", sorted(words))
    print("Strings sorted (case-insensitive):", sorted(words, key=str.lower))

Output

stdout
Original list: [42, 17, 8, 99, 23, 5]
Ascending: [5, 8, 17, 23, 42, 99]
Descending: [99, 42, 23, 17, 8, 5]
Original after sorted() still unchanged: [42, 17, 8, 99, 23, 5]
In-place sorted: [1, 3, 8, 10]
Strings sorted (case-sensitive): ['Banana', 'apple', 'cherry', 'date']
Strings sorted (case-insensitive): ['apple', 'Banana', 'cherry', 'date']

How it works

The sorted() function returns a brand new list, leaving the original untouched — this makes it safe for working with data you still need in its original order. The reverse=True parameter reverses the sort direction without extra code. The list.sort() method modifies the list in place and returns None, which is ideal when memory matters and you want to sort without copying. The optional key parameter accepts a function like str.lower to customize the sort criteria. Both approaches sort numbers and strings, but be careful with mixed types since Python will raise a TypeError if items aren't comparable.

Common mistakes

  • Confusing `sorted()` (returns new list) with `.sort()` (modifies in place and returns `None`)
  • Forgetting that `.sort()` returns `None`, so assigning its result to a variable gives `None` instead of a list
  • Sorting a list with mixed data types (e.g., ints and strings) causes a `TypeError`
  • Assuming case-insensitive sorting is the default for strings — it's actually case-sensitive

Variations

  1. Use `sorted(data, key=len)` to sort strings by their length
  2. Use `data.sort(reverse=True)` to sort with the in-place method in descending order

Real-world use cases

  • Sorting user records or log entries by timestamp before displaying them in a UI dashboard.
  • Ordering product prices or inventory counts from lowest to highest for reporting.
  • Sorting API response data by a field like name or rating before storing or passing it downstream.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.