To sort a list of strings using Python, the simplest way is to use the Python sort() function.
list_object.sort()
You can also sort a list of string using the Python sorted() function.
sorted_list_object = sorted(list_object)
Let’s say I have the following list of strings that I want to sort in Python:
l = ["this","is","a","list","of","strings"]
We can sort the list of strings with the sort() function. The sort() function manipulates our list object directly.
l = ["this","is","a","list","of","strings"]
l.sort()
print(l)
#output:
["a","is","list","of","strings","this"]
We can also sort a list of strings using the sorted() function. The sorted() function takes a list of objects as a parameter and returns a new list which is sorted.
l = ["this","is","a","list","of","strings"]
new_l = sorted(l)
print(new_l)
#output:
["a","is","list","of","strings","this"]
Sorting a List of Strings in Descending Order in Python
Sometimes, when we have a list of strings, we want to sort the strings in descending order. We can sort the list in descending order by passing a value for the “reverse” parameter to the sort() function.
Let’s say I have the following list of strings:
l = ["this","is","a","list","of","strings"]
To sort the list of strings in descending order, we can pass “reverse=true” to the sort() function. The following code will sort a list of strings in reverse:
l = ["this","is","a","list","of","strings"]
l.sort(reverse=true)
print(l)
#output:
["this","strings","of","list","is","a"]
We can sort the list of strings in descending order using the sorted() function by passing “reverse=true”.
The code below shows how you would use the sorted() function to sort a list of strings in reverse.
l = ["this","is","a","list","of","strings"]
new_l = sorted(l,reverse=true)
print(new_l)
#output:
["this","strings","of","list","is","a"]
Sorting a List of Integer Strings in Python
If we have a list of strings which are integers, we can sort the list by passing a value for the “key” parameter to the sort() function.
Let’s say I have the following list of strings. The strings in this list are all integer numbers.
l = ["100","12","34","1","5","56","78"]
To sort the list of strings made up of integers, we can pass “key=int” to the sort() function. The following code will sort a list of strings made up of integers:
l = ["100","12","34","1","5","56","78"]
l.sort(key=int)
print(l)
#output:
["1","5","12","34","56","78","100"]
We can sort the list of strings made up of integers using the sorted() function by passing “key=int”.
The code below shows how you would use the sorted() function to sort a list of strings which are integers.
l = ["100","12","34","1","5","56","78"]
new_l = sorted(l,key=int)
print(new_l)
#output:
["1","5","12","34","56","78","100"]
You can also combine “reverse=true” with “key=int” to sort the list of strings made up of integers in descending order.
The code below shows you how to sort a list of strings which are integers in reverse:
l = ["100","12","34","1","5","56","78"]
new_l = sorted(l,key=int, reverse=true)
print(new_l)
#output:
["100","78","56","34","12","5","1"]
Sorting a List of Strings with Another Function in Python
Something really cool about the sort() and sorted() functions is that the “key” parameter can take any function as input.
In the example above with “key=int”, the sorted function is applying the Python int() function to each of the string elements and then sorting.
We can define our own function to sort a list of strings. Let’s say I have the following list of strings:
l = ["this","is","another","list","of","strings"]
Let’s say I want to sort the list of strings by the second letter of each word. We define a function which returns the second letter of a string, and we pass that to the “key” parameter in the sorted() function as shown below:
l = ["this","is","another","list","of","strings"]
def second_letter(s):
return s[1]
new_l = sorted(l,key=second_letter)
print(new_l)
#output:
['of', 'this', 'list', 'another', 'is', 'strings']
Hopefully this article has helped you understand how to sort a list of strings in Python.