Why does a list not sort properly in Python as expected?
There are a couple of reasons why a list may not have sorted as expected in Python. Here are a couple of ways to diagnose the problem to help you check.
Here’s a quick list (no pun intended!) you can use to quickly check:
Are all elements within the list of the same data type?
Look at setting them to the same data type using the
- Look at setting them to the same data type using the
Are you capturing the result of a
Change this sort operation to the
- Change this sort operation to the
Are you sorting items with mixed case?
keyparameter converting all items to
- Apply the
Do you understand the
lambdafunction to check it works on elements in your list.
- Extract the
Are you sorting in reverse?
reverseparameter to your sort function.
Falseis the default setting it this parameter is not defined.
- Check the
Are you sorting unicode characters?
Look to use a standard encoding type with the
lambda x : x.encode('utf-8').
- Look to use a standard encoding type with the
Are there any whitespace characters wrapping the elements?
.strip()string method to each element with the
- Apply the
Let’s explore each of these items in a little more detail with examples. Here are some reasons why your Python lists may not be sorting properly:
Data Type Inconsistencies
Understanding data types in Python is crucial to ensure your list sorts properly. Python supports several data types, such as integers, floats, and strings. When a list contains mixed data types, Python may not perform the sorting as expected.
How Python sorts data types differently depends on the items in the list. For example, a list with both integers and strings will raise a
when you try to sort it. To avoid this issue, you must handle mixed data types in your lists properly.
Once way of doing this is to use the
parameter in the
list methods that can enable you to convert each of the elements within a list to the same data type:
>>> my_list = [2023, '2022', 2020.0] >>> sorted(my_list, key=lambda x: str(x)) [2020.0, '2022', 2023]
Tips for handling mixed data types in lists include converting all items to a uniform data type or using a custom sorting function to order items based on your requirements.
Function Used Incorrectly
Python offers two main ways to sort lists: the
method, which sorts a list in place, and the
function, which returns a new sorted list without altering the original. Understanding the difference between these two options is essential for avoiding sorting issues.
Common mistakes when using
involve invoking them incorrectly, resulting in an unsorted list or unexpected changes to the original list. To ensure you’re using these functions properly, pay close attention to their syntax and return values.
One recent example where I made a mistake was expecting the
list method to return a result (i.e. treating the sort operation like the
Here’s an example of this mistake:
>>> my_list = [3, 2, 1] >>> my_sorted_list = my_list.sort() >>> print(my_sorted_list) None
What I should have done with the above code was use the
>>> my_list = [3, 2, 1] >>> my_sorted_list = sorted(my_list) >>> print(my_sorted_list) [1, 2, 3]
It helps if you stick to one method when sorting a list as this helps you to minimise the impact of this type of error occurring. Of course, this error will also just creep in because you may just simply forget!
My preference is to use the
method, as it doesn’t modify the original list.
Performing Case-Insensitive Sorting
When sorting string lists, Python’s default behaviour sorts uppercase letters before lowercase, potentially confusing the sort order.
To perform case-insensitive sorting, use the
argument with the
function as your key:
>>> surnames = ['de Longi', 'Deloitte', 'de Angelo'] >>> sorted(surnames) ['Deloitte', 'de Angelo', 'de Longi']
To fix this issue use the
parameter with a
function that converts each element in the list to lowercase:
>>> surnames = ['de Longi', 'Deloitte', 'de Angelo'] >>> sorted(surnames, key=lambda x: x.lower()) ['de Angelo', 'de Longi', 'Deloitte']
parameter is a powerful parameter in your sort functions, however, if you don’t know what it’s doing then it could be causing all weird and wonderful types of sorting.
If you need to sort elements based on a specific attribute or function, you can use the
argument in the
function. This will allow you to define a custom sort key that will be used to determine the order of elements.
However, if you don’t know what it’s doing then you could be in all manner of confusion. Extract the
function out from the
parameter and replace the variable
with a string to see what it does.
>>> my_list = [3, 'a', 1.0] >>> sorted(my_list, key=lambda x: ord(str(x).lower())) [1.0, 3, 'a']
So to find out what on earth is happening in the
function extract it and swap out the variable used (in my case
) for a string or element in your list:
>>> ord(str('Test')) 116
You can even step through the function by examining the inner most functions by breaking it apart:
>>> str('Test').lower() 'test' >>> 'test' 't' >>> ord('t')
Ah, ok. I’m finding the ordinal number of the first character of each element when it’s converted to a string.
Sorting In Reverse Order
Whether using the
function, you can easily sort your list in reverse order by adding the
This will help you obtain a descending order when sorting your list items.
Check if you are using this parameter in your sort function. By default if the
parameter isn’t used then the sort function will sort the items in
will sort the items in
Character Encoding Issues
Locale settings and character encoding can affect the way your Python list is sorted. You can use the
parameter and set each element to a specific encoding should it help.
>>> my_list = ['♠', 'Ω', 'и', '\u1118'] >>> sorted(my_list, key=lambda x: x.encode('utf-8')) ['Ω', 'и', 'ᄘ', '♠']
Python uses your system’s locale settings to determine appropriate sorting rules for characters. Checking and updating your locale settings may help resolve sorting problems related to special characters or accented letters.
Check elements aren’t padded with whitespace as this can distort your results. Here’s an example where whitespace is giving incorrect sorts:
>>> my_list = [' 1', '\n2', '\t3'] >>> sorted(my_list) ['\t3', '\n2', ' 1']
Once again the
parameter can help by performing a clean on each data element by stripping away any leading and trailing whitespace.
>>> my_list = [' 1', '\n2', '\t3'] >>> sorted(my_list, key=lambda x: x.strip()) [' 1', '\n2', '\t3']
As you can see the above result produces the intended result by sorting the strings accordingly with the leading whitespace
Python List Not Sorting – Summary
If your Python sorting operation isn’t working out as you think perform the following checks to see if something isn’t set properly: data types; capturing result of
; mixed cases; complex
function used; sorting in reverse; unicode characters; and leading whitespace characters.
To explore more about lists, read my next article on sorting a list alphabetically .