In this Python tutorial, you will learn how to sort a given list of strings using sort() or sorted() builtin functions.
Python – Sort List of Strings
There are different ways to sort a Python list of strings in ascending or descending order.
Following are some of the ways we shall discuss to sort strings in Python.
- Using sort() method. Or
- Using sorted() of method.
Sort list of strings using list.sort()
sort() is a method that can be applied only on lists. It is defined in list class.
sort() method modifies the original list. So, if it required to keep the original list, you may copy the list and then sort it.
The syntax of list.sort() is:
sort(*, key=None, reverse=False)
1. Sort list of strings in ascending order
In the following example, we shall use list.sort() method to sort a list of strings in ascending order.
Python Program
words = ["car", "apple", "banana", "fan", "dog"]
#sort list in ascending order
words.sort()
print(words)
Output
['apple', 'banana', 'car', 'dog', 'fan']
sort() method considers ASCII values of the characters while comparing strings. Hence, sort()
treats a
and A
as different characters and a
is greater than A
, since ASCII value of a
is 97
and A
is 65
.
Reference tutorials for the above program
2. Sort list of strings that contain upper and lowercase letters
Let us take the list in above example, and include the words with capital case letters.
Python Program
words = ["car", "apple", "banana", "Car"]
#sort list in ascending order
words.sort()
print(words)
Output
['Car', 'apple', 'banana', 'car']
You can apply a function of one argument on the words only for comparison purpose, using key
argument to sort()
method.
3. Sort list of strings with transformed value for comparison
We will use the same list as in the above example, and convert the words to uppercase only for comparison purpose. This uppercase string values are used for string comparison only while sorting. And because of that, the strings car
and Car
, when compared, would be same.
Python Program
words = ["car", "apple", "banana", "Car"]
#sort list in ascending order
words.sort(key=str.upper)
print(words)
In key=str.upper
, str
represents this list item.
Output
['apple', 'banana', 'car', 'Car']
4. Sort list of strings in descending order
By default list.sort() method sorts the list in ascending order.
We can also sort list in descending order by passing reverse=True
to list.sort()
.
Python Program
words = ["car", "apple", "banana"]
#sort list in descending order
words.sort(reverse=True)
print(words)
Output
['car', 'banana', 'apple']
Sort list of strings using sorted() function
sorted() function can be applied on any iterable. In this tutorial, we will pass a list of strings to sorted() function to sort the list in ascending or descending order.
sorted(iterable, *, key=None, reverse=False)
1. Sort using sorted() builtin function
In the following example, we will use sorted() function to sort a list in ascending order.
Python Program
words = ["car", "apple", "banana"]
#sort list in ascending order
sortedList = sorted(words)
print(words)
print(sortedList)
Output
['car', 'apple', 'banana']
['apple', 'banana', 'car']
The original list is unmodified. sorted() returns a list that is sorted in ascending order by default.
Reference tutorials for the above program
You can sort the list in descending order in the same as that of sort() method where we passed reverse=True
.
Also, you can specify a function that takes a single argument to apply on the list items while only making the comparison.
Difference between sort() and sorted()
Following table gives the differences between list.sort() method and sorted() function.
sort() | sorted() |
list.sort() method is defined only for lists. | sorted() function accepts any iterable. |
sort() modifies the list in-place. | sorted() function creates a new list from the iterable. |
Conclusion
In this Python Tutorial, we learned how to sort a list of strings in ascending or descending order using list.sort() method and sorted() function.