In python, we use strings to process text data. While programming, we may need to sort a list of strings in python. In this article, we will discuss different ways to sort a list of strings alphabetically in python.
Sort List of Strings Alphabetically using the sort() method.
To sort elements of a list, we use the sort()
method. When invoked on a list, the sort()
method sorts the elements of the existing list. For instance, we can sort a list of numbers as follows.
myList = [1, 23, 12, 345, 34, 45]
print("The list is:")
print(myList)
myList.sort()
print("The reverse sorted List is:")
print(myList)
Output:
The list is:
[1, 23, 12, 345, 34, 45]
The reverse sorted List is:
[1, 12, 23, 34, 45, 345]
To sort a list of strings alphabetically, we can use the sort()
method in the same way as we use it to sort a list of numbers. The strings in the list will be compared according to their first character. The string with the first character that comes first in the ASCII value will be in the first position in the sorted list. For example, “apple” will come before “box” as “a” comes before “b”. You can observe this in the following example.
myList = ["apple","box","tickle","python","button"]
print("The list is:")
print(myList)
myList.sort()
print("The sorted List is:")
print(myList)
Output:
The list is:
['apple', 'box', 'tickle', 'python', 'button']
The sorted List is:
['apple', 'box', 'button', 'python', 'tickle']
If two strings have the same first character, the strings will be compared using the second character. Similarly, if the second character is same for two strings, they will be ordered in the sorted list on the basis of their third character and so on. You can observe this in the following example.
myList = ["apple", "aaple", "aaaple", "tickle", "python", "button"]
print("The list is:")
print(myList)
myList.sort()
print("The sorted List is:")
print(myList)
Output:
The list is:
['apple', 'aaple', 'aaaple', 'tickle', 'python', 'button']
The sorted List is:
['aaaple', 'aaple', 'apple', 'button', 'python', 'tickle']
You can also sort the list of strings in reverse alphabetical order by using the parameter “reverse
” as follows.
myList = ["apple", "aaple", "aaaple", "tickle", "python", "button"]
print("The list is:")
print(myList)
myList.sort(reverse=True)
print("The reverse sorted List is:")
print(myList)
Output:
The list is:
['apple', 'aaple', 'aaaple', 'tickle', 'python', 'button']
The reverse sorted List is:
['tickle', 'python', 'button', 'apple', 'aaple', 'aaaple']
Sort List of Strings Alphabetically using the sorted() Function
If you don’t want to modify the existing list, you can use the sorted()
function to sort the list of strings in python. The sorted()
function works in a similar way to the sort()
method. The only difference is that it returns a new sorted list instead of modifying the original list.
To sort the list of strings alphabetically, we will pass the list of strings to the sorted() function and it will return the sorted list as follows.
myList = ["apple", "aaple", "aaaple", "tickle", "python", "button"]
print("The list is:")
print(myList)
newList = sorted(myList)
print("The sorted List is:")
print(newList)
Output:
The list is:
['apple', 'aaple', 'aaaple', 'tickle', 'python', 'button']
The sorted List is:
['aaaple', 'aaple', 'apple', 'button', 'python', 'tickle']
Here, the mechanism for sorting the strings is similar to what we discussed in the previous section.
You can also sort the list in reverse alphabetical order by using the “reverse
” parameter as follows.
myList = ["apple", "aaple", "aaaple", "tickle", "python", "button"]
print("The list is:")
print(myList)
newList = sorted(myList, reverse=True)
print("The reverse sorted List is:")
print(newList)
Output:
The list is:
['apple', 'aaple', 'aaaple', 'tickle', 'python', 'button']
The reverse sorted List is:
['tickle', 'python', 'button', 'apple', 'aaple', 'aaaple']
Conclusion
In this article, we have discussed how to sort a list of strings alphabetically in python using the sort()
method and the sorted()
function. We also discussed how the sort()
method and the sorted()
function operate while sorting the list of strings. To read more about strings, you can read this article on string concatenation. You might also like this article on list comprehension.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.