How to Sort Python Lists Permanently with sort()
Learn how to permanently reorder Python lists with list.sort(), verify the changed order, and sort strings in reverse alphabetical order with reverse=True.
What permanent sorting means
A list is a mutable, ordered Python collection. Mutable means that the collection can be changed after it is created.
Permanent sorting means changing the existing list object so that its elements remain in the new order. Python's list.sort() method performs this change in place. An in-place operation changes an existing object instead of producing a new object.
After sort() runs, later indexing, printing, iteration, and other list operations use the reordered arrangement. The same list variable still refers to the same list, but the elements are now in a different order.
Using the list.sort() method
Call the method with dot notation:
list_name.sort()The method sorts the elements of a mutable list in ascending order by default. It changes the list directly and returns None, not a separate sorted list.
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
last_names.sort()
print(last_names)Output:
['Antunovich', 'Daniels', 'Jones', 'Thomson']The original last_names list has been reordered. You do not need to assign the result of sort() to another variable.
Ascending alphabetical order
For comparable strings, the default sorting order is ascending order. For text, this generally means alphabetical order from A to Z. The alphabetically earliest item appears first.
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
print('Before:', last_names)
last_names.sort()
print('After:', last_names)
print('First surname:', last_names[0])
Output:
Before: ['Jones', 'Antunovich', 'Daniels', 'Thomson']
After: ['Antunovich', 'Daniels', 'Jones', 'Thomson']
First surname: AntunovichAn index is a numeric position used to access a list element. Python uses zero-based indexes, so index 0 refers to the first element. Because the list remains sorted after the method call, last_names[0] is still 'Antunovich' later in the program.
Permanent and temporary sorting compared
The related sorted() function creates a new sorted result. It does not change the original iterable. Use sort() when the existing list should adopt the new order.
| Approach | Original List Modified | Return Value | Typical Use |
|---|---|---|---|
list.sort() | Yes | None | Reorder the existing mutable list |
sorted(iterable) | No | A new sorted list | Keep the original order and use a separate result |
For example:
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
sorted_names = sorted(last_names)
print(last_names)
print(sorted_names)
Here, last_names keeps its original order, while sorted_names contains the alphabetical order. This lesson focuses on sort(), which permanently changes the list's arrangement.
Sorting in descending order with reverse=True
Descending order is the reverse of the default sorted order. For strings, it generally means reverse alphabetical order from Z to A.
Pass the keyword argument reverse=True:
list_name.sort(reverse=True)The argument tells sort() to reverse the normal sorted order.
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
last_names.sort(reverse=True)
print(last_names)
print(last_names[0])
Output:
['Thomson', 'Jones', 'Daniels', 'Antunovich']
ThomsonAfter reverse sorting, 'Thomson' is first because it is the alphabetically latest surname in this list.
| Call | Order for Text | Example First Element |
|---|---|---|
list.sort() | Ascending, usually A to Z | 'Antunovich' |
list.sort(reverse=True) | Descending, usually Z to A | 'Thomson' |
How to verify that the list changed
Print the list before and after sorting, then access an element by index. This makes the in-place change visible.
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
print('Before sorting:', last_names)
last_names.sort()
print('After sorting:', last_names)
print('Element at index 0:', last_names[0])
The variable last_names now refers to the reordered list. No replacement list was assigned to it.
Common mistakes and troubleshooting
Assigning sort() back to the variable
This pattern is incorrect:
last_names = last_names.sort()sort() changes the list in place and returns None. The assignment replaces the list variable's value with None.
Call the method on its own line instead:
last_names.sort()
print(last_names)Expecting the original order to remain
Calling sort() permanently changes the order of the existing list. If the original order is needed later, make a copy before sorting or use sorted() to create a separate result.
last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
original_order = last_names.copy()
last_names.sort()
print(original_order)
print(last_names)Getting ascending order instead of descending order
If reverse alphabetical order is intended, include the keyword argument:
last_names.sort(reverse=True)Unexpected ordering with uppercase and lowercase text
Python compares string characters according to their ordering rules, so capitalization can affect the result. For case-insensitive ordering, an optional next step is to provide a key function:
names = ['zoe', 'Alice', 'bob']
names.sort(key=str.lower)
print(names)The key=str.lower argument tells Python to compare lowercase versions of the strings while keeping the original values in the list.
Key points
list.sort()sorts a mutable list in place.- The default order is ascending, usually alphabetical A to Z for strings.
sort()returnsNone, so call it without assigning its result.- Use
reverse=Truefor descending or reverse alphabetical order. - After sorting, indexing such as
list_name[0]uses the new order. - Use
sorted()or a copy when the original ordering must be preserved.