Sort List of Strings in Python (2 Examples)
In this tutorial, I’ll demonstrate how to sort a list of strings in the Python programming language.
The table of content looks as follows:
Let’s dive into it!
At the start, we’ll need to create some data that we can use in the following examples. It is a Python list containing six strings.
string_list = ["Statistics", "Data", "Science", "Python", "String", "Hello"] # generating an example float list
Example 1: Sorting using sort() Function
Example 1 illustrates how to sort a list of strings in ascending order using the sort() function.
string_list.sort() # sorting the list ascending print(string_list) # printing the sorted list # ['Data', 'Hello', 'Python', 'Science', 'Statistics', 'String']
Notice that the list was sorted in place, which means that a new object was not created, instead, string_list became sorted. As an alternative, the list can also be sorted in descending order by setting the reverse argument to True, see below.
string_list.sort(reverse=True) # sorting the list descending print(string_list) # printing the sorted list # ['String', 'Statistics', 'Science', 'Python', 'Hello', 'Data']
Now you see that the order of the elements in string_list has been reversed.
Example 2: Sorting using sorted() Function
Example 2 explains how to sort a list of strings with the use of the sorted() function.
print(sorted(string_list)) # sorting the list and printing # ['Data', 'Hello', 'Python', 'Science', 'Statistics', 'String']
The unique thing about the sorted() function is that it generates a new sorted list object, which contains the elements of the original list in an ordered way. See how printing string_list returns the unordered result in the beginning.
print(string_list) ['Statistics', 'Data', 'Science', 'Python', 'String', 'Hello']
Now let’s order the list in reversed order. Just like in example 1, the reverse argument helps to implement it.
print(sorted(string_list, reverse=True)) # sorting in reverse and printing # ['String', 'Statistics', 'Science', 'Python', 'Hello', 'Data']
Here you go, now the elements are in reverse order.
Video & Further Resources
I have recently released a video on my YouTube channel, which shows the Python syntax of this article. You can find the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Besides the video, you could have a look at the other tutorials on this website. A selection of articles that are related to the sorting of a list of strings is shown below.
- Using Lists in Python (Introduction)
- Sort List of Integers in Python
- Sort List of Floats in Python
- Access List Index in for Loop in Python
- Access Multiple List Elements by Index in Python
- Python Programming Overview
In summary: At this point, you should know how to create a sorted list of strings in Python programming. Don’t hesitate to let me know in the comments section if you have additional questions or comments.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter