Sorting HOW TO: Guido Van Rossum and The Python Development Team
Sorting HOW TO: Guido Van Rossum and The Python Development Team
Release 3.4.3
Contents
1
Sorting Basics
Key Functions
1 Sorting Basics
A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
You can also use the list.sort() method. It modifies the list in-place (and returns None to avoid confusion).
Usually its less convenient than sorted() - but if you dont need the original list, its slightly more efficient.
>>>
>>>
>>>
[1,
a = [5, 2, 3, 1, 4]
a.sort()
a
2, 3, 4, 5]
Another difference is that the list.sort() method is only defined for lists. In contrast, the sorted()
function accepts any iterable.
>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]
2 Key Functions
Both list.sort() and sorted() have a key parameter to specify a function to be called on each list element
prior to making comparisons.
For example, heres a case-insensitive string comparison:
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
The value of the key parameter should be a function that takes a single argument and returns a key to use for
sorting purposes. This technique is fast because the key function is called exactly once for each input record.
A common pattern is to sort complex objects using some of the objects indices as keys. For example:
>>> student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
>>> sorted(student_tuples, key=lambda student: student[2])
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
# sort by age
The same technique works for objects with named attributes. For example:
>>> class Student:
def __init__(self, name, grade, age):
self.name = name
self.grade = grade
self.age = age
def __repr__(self):
return repr((self.name, self.grade, self.age))
>>> student_objects = [
Student('john', 'A', 15),
Student('jane', 'B', 12),
Student('dave', 'B', 10),
]
>>> sorted(student_objects, key=lambda student: student.age)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
# sort by age
The Timsort algorithm used in Python does multiple sorts efficiently because it can take advantage of any ordering
already present in a dataset.