To sort a list of strings according to their length, use a
lambda
function on the
key
parameter with the
lambda
function using the
len
function on each element. This is represented as follows using the
sorted
function:
sorted(my_list, key=lambda el: len(el))
.
Here is a simple example demonstrating the use of this code:
my_list = ['Smithers', 'Smiths', 'Sims', 'Smalls']
my_sorted_list = sorted(my_list, key=lambda el: len(el))
print(my_sorted_list)
# ['Sims', 'Smiths', 'Smalls', 'Smithers']
Or, if using the
.sort()
list method would be represented as follows:
my_list = ['Smithers', 'Smiths', 'Sims', 'Smalls']
my_list.sort(key=lambda el: len(el))
print(my_list)
# ['Sims', 'Smiths', 'Smalls', 'Smithers']
What if after having the list sorted by their length you wanted to sort those elements that had the same length using another condition?
How do you add multiple conditions to your sorting criteria?
Sorting By String Length And Then Alphabetically
To add another sorting condition to your sort function change the result of the lambda function to a tuple that contains the order of the sorting you want to perform.
For example, if you wanted to sort the elements within the list by their string length and then alphabetically, you would modify your lambda function to the following:
lambda el: (len(el), el)
The first element in the tuple represents the initial sort of each element’s length. The second element in the tuple is the next condition which will sort the elements alphabetically.
my_sorted_list = sorted(my_list, key=lambda el: (len(el), el))
print(my_sorted_list)
# ['Sims', 'Smalls', 'Smiths', 'Smithers']
How To Order By Largest String Lengths First
To order the length of each element in the list by placing the longest strings at the front, simply use the following
sorted
function:
sorted(my_list, key=lambda el: -len(el))
.
Here is the result of the code as mentioned when sorting by largest string lengths first:
my_sorted_list = sorted(my_list, key=lambda el: -len(el))
print(my_sorted_list)
# ['Smithers', 'Smiths', 'Smalls', 'Sims]
If you compare this code with the previous did you notice the one character inserted into this code that differed from the original?
It’s the minus sign in front of the
len()
function.
If you’re looking to reverse the order of each condition in your sorting criteria place a negative sign if you want to reverse the results of that condition.
To insert any additional conditions into your sort criteria wrap the
-len(el)
in a tuple and add any further conditions. For example, if you want to sort the value alphabetically then your function would be as follows:
# sort list by longest first, and then alphabetically
sorted(my_list, key=lambda el: (-len(el), el))
Summary
To sort a list by the length of each string element without using any imported libraries use the native
.sort()
list method or
sorted()
function. Whichever function or method is used set the
key
parameter to reference a
lambda
function that references the element being passed and use the
len()
function to determine the length of each string.
If you want to see other examples of lists being sorted check out the sorting of two-dimensional lists in Python without using any imported libraries.