Sorting Python Lists: sort() vs sorted() & Custom Methods

1. Introduction

Python is widely used as a simple and intuitive programming language. Among its features, “list sorting” is one of the basic and frequently used operations. This article provides a detailed explanation of how to sort lists in Python, as well as customization methods and cautions. By reading this article, even beginners will understand how to sort lists efficiently and be able to apply it in real programs.

2. How to Sort Lists

Python has two main sorting methods. Let’s look at the characteristics and usage of each.

2.1 Sorting using the sort() method

sort() method is a method provided on list objects, and it directly rearranges the original list. By default it sorts in ascending order (small to large), but specifying reverse=True makes it sort in descending order (large to small).

Basic Usage

numbers = [3, 1, 4, 1, 5]
numbers.sort()
print(numbers)  # Output: [1, 1, 3, 4, 5]

When sorting in descending order

numbers = [3, 1, 4, 1, 5]
numbers.sort(reverse=True)
print(numbers)  # Output: [5, 4, 3, 1, 1]

2.2 Sorting using the sorted() function

sorted() function returns a new sorted list without modifying the original list. It also defaults to ascending order, and you can get descending order by using reverse=True.

Basic Usage

numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [1, 1, 3, 4, 5]
print(numbers)  # Output: [3, 1, 4, 1, 5] The original list is unchanged

Sort in descending order

numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)  # Output: [5, 4, 3, 1, 1]
年収訴求

3. Customizing Sort

In Python, using the key argument, custom sort is possible. It is useful when you want to reorder a list with your own rules.

3.1 Sorting using the key argument

Sorting by String Length

When the elements in a list are strings, specifying the len function in key will sort based on string length.
words = ['banana', 'apple', 'cherry', 'date']
words.sort(key=len)
print(words)  # ['date', 'apple', 'banana', 'cherry']

3.2 Sorting a List of Dictionaries

When sorting a list that contains dictionaries, using the lambda function with the key argument allows sorting based on a specific key.

Sorting by Numeric Values in Dictionaries

fruits = [
    {'name': 'apple', 'price': 100},
    {'name': 'banana', 'price': 50},
    {'name': 'cherry', 'price': 150}
]

fruits.sort(key=lambda x: x['price'])
print(fruits)
# [{'name': 'banana', 'price': 50}, {'name': 'apple', 'price': 100}, {'name': 'cherry', 'price': 150}]

Sorting with Multiple Criteria

If you want to sort by name when prices are equal, specify a tuple in key.
fruits.sort(key=lambda x: (x['price'], x['name']))
print(fruits)
# [{'name': 'banana', 'price': 50}, {'name': 'apple', 'price': 100}, {'name': 'cherry', 'price': 150}]

4. Sorting Considerations

4.1 When Different Data Types Are Mixed

If a list contains both numbers and strings, TypeError occurs. Be careful.
mixed_list = [3, 'apple', 2]
mixed_list.sort()
# Output: TypeError: '<' not supported between instances of 'str' and 'int'

Solution

Either unify the data types or use a custom sort.
mixed_list = [3, 'apple', 2]
mixed_list = [str(x) for x in mixed_list]
mixed_list.sort()
print(mixed_list)  # ['2', '3', 'apple']

4.2 Stability of Sorting

Python’s sort is a “stable sort”. When duplicate values exist, the original list order is preserved.
data = [(1, 'a'), (2, 'b'), (1, 'c')]
data.sort(key=lambda x: x[0])
print(data)  # [(1, 'a'), (1, 'c'), (2, 'b')]
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

5. TimSort: Python’s default sorting algorithm

In Python, the TimSort (Tim sort) algorithm is used for sorting lists. It is a stable sorting algorithm that combines merge sort and insertion sort.

Features of TimSort

  1. Stable sort: The order of elements with equal values is preserved.
  2. Fast processing: It performs efficiently on real-world data sets and is fast in average cases.
  3. Strong with partially sorted data: Performance especially improves on data that is already partially ordered.

6. Performance Comparison of sort() and sorted()

Because the sort() method and sorted() function in Python have operational differences, let’s also understand the performance.

Fundamental Differences

Featuresort()sorted()
TargetModifies the original listCreates a new list
Return valueNone (returns None)New sorted list
Use caseWhen it’s okay to modify the listWhen you want to keep the original list

Performance Comparison

sort() is slightly faster than sorted(). Because sort() sorts the list in place, there is no additional memory cost.

Benchmark Example

import time

numbers = [3, 1, 4, 1, 5] * 100000

# Timing sort()
start = time.time()
numbers.sort()
end = time.time()
print("sort() processing time:", end - start)

# Timing sorted()
numbers = [3, 1, 4, 1, 5] * 100000
start = time.time()
sorted_numbers = sorted(numbers)
end = time.time()
print("sorted() processing time:", end - start)
Thus, it is important to choose sort() and sorted() appropriately based on the use case.

7. Techniques for Sorting Lists

Simple way to sort in reverse order

There is also a way to sort the list in reverse order without using sort() and sorted().
numbers = [1, 2, 3, 4, 5]
reverse_numbers = numbers[::-1]
print(reverse_numbers)  # Output: [5, 4, 3, 2, 1]

8. Common Errors and SolutionsTypeError: ‘<‘ not supported between instances

  • Cause: When the list contains different data types (e.g., numbers and strings).
  • Solution: Standardize the data types or handle them explicitly with a custom sort.
Example
# Code that raises an error
mixed_list = [3, 'apple', 2]
mixed_list.sort()  # TypeError

# Solution
mixed_list = [str(x) for x in mixed_list]
mixed_list.sort()
print(mixed_list)  # ['2', '3', 'apple']

9. Frequently Asked Questions (FAQ)

Q1: What is the difference between sort() and sorted()?

A:
  • sort() method: It directly rearranges the contents of the list. The original list is modified.
  • sorted() function: It returns a new sorted list. The original list is not changed.
Example
numbers = [3, 1, 4, 1, 5]

# Using sort()
numbers.sort()
print(numbers)  # [1, 1, 3, 4, 5] - the original list is modified

# Using sorted()
numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # [1, 1, 3, 4, 5]
print(numbers)  # [3, 1, 4, 1, 5] - the original list is not changed

Q2: How can I sort in descending order?

A: You can sort in descending order by specifying reverse=True to the sort() method or the sorted() function. Example
numbers = [3, 1, 4, 1, 5]

# Sort in descending order with sort()
numbers.sort(reverse=True)
print(numbers)  # [5, 4, 3, 1, 1]

# Sort in descending order with sorted()
numbers = [3, 1, 4, 1, 5]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)  # [5, 4, 3, 1, 1]

Q3: What is a custom sort? How do you use it?

A: A custom sort is a way to define your own sorting criteria. You specify a function or a lambda expression using the key argument. For example, you can sort based on string length or a specific key in a dictionary. Example: Sort by string length
words = ['banana', 'apple', 'cherry', 'date']
words.sort(key=len)
print(words)  # ['date', 'apple', 'banana', 'cherry']

Q4: What sorting algorithm does Python use?

A: Python uses a sorting algorithm called TimSort. It is an algorithm that combines merge sort and insertion sort, and it is efficient for partially sorted data.

Q5: Are there ways to handle slow sorting?

A: When the list has a very large number of elements or you perform complex custom sorts, the processing speed can become slow. You can improve it using the following methods.
  1. Limit sorting to the minimum necessary Avoid unnecessary sorting and try to sort only a portion of the data.
  2. Use the heapq module By using heapq from the Python standard library, you can efficiently perform partial sorting (e.g., retrieving the top N items).
Example: Retrieve the top 3 numbers
import heapq

numbers = [5, 1, 8, 3, 2, 9]
top3 = heapq.nlargest(3, numbers)
print(top3)  # [9, 8, 5]
年収訴求