Sort a list of strings in lexicographical order in Python
Last Updated :
14 Apr, 2025
We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:
Input: ["banana", "apple", "cherry", "date"]
Output:
Increasing: ['apple', 'banana', 'cherry', 'data']
Decreasing: ['data', 'cherry', 'banana', 'apple']
Let's look at different ways of doing it, with examples:
Using Sort()
sort() method sorts a list in place, meaning it directly modifies the original list. Using reverse=True sorts the list in decreasing order. Example:
Python
a = ["banana", "apple", "cherry", "date"]
a.sort()
print(a)
Output['apple', 'banana', 'cherry', 'date']
Case Sensitivity in Lexicographical Sorting
Python’s sort() function is case-sensitive, which means uppercase words are sorted before lowercase ones. To perform a case-insensitive sort, we can use str.lower function as the sorting key.
Python
a = ["Banana", "apple", "Cherry", "date"]
a.sort(key=str.lower)
print(a)
Output['apple', 'Banana', 'Cherry', 'date']
Using sorted()
sorted() function is similar to sort() but instead of modifying the list in-place, it returns a new sorted list. This is useful when we want to keep the original list unchanged. Like sort(), sorted() can also take key and reverse parameters for customized sorting: Example:
Python
a1 = ["banana", "apple", "cherry", "date"]
b = sorted(a1)
print(b)
a2 =["banana", "Apple", "cherry", "Date"]
c = sorted(a2, key=str.lower)
print(c)
d = sorted(a2, key=str.lower, reverse=True)
print(d)
Output['apple', 'banana', 'cherry', 'date']
['Apple', 'banana', 'cherry', 'Date']
['Date', 'cherry', 'banana', 'Apple']
Explanation:
- c = sorted(a2, key=str.lower) line sorts the list a in a case-insensitive manner by using str.lower as the key function, resulting in a new sorted list c.
- d = sorted(a2, key=str.lower, reverse=True) line sorts the list a in reverse lexicographical order, creating a new list d with the elements sorted from highest to lowest.
Using heapq.nlargest()
heapq.nlargest() is used to find the largest items in a list. While it works well for picking a few top items, it's not the best choice for sorting the entire list in reverse order. Example:
Python
import heapq
a = ["banana", "apple", "cherry", "date"]
res = heapq.nlargest(len(a), a) # Returns the list sorted in reverse order
print(res)
Output['date', 'cherry', 'banana', 'apple']
Explanation:
- heapq.nlargest(n, iterable) returns the top n largest elements in descending order.
- Since strings are compared lexicographically, this gives words from Z to A.
- Using len(a) returns all elements, so it works just like sorted(a, reverse=True).
For in-depth knowledge about functions used in this article, refer to: sort(), sorted(), heapq.nlargest().
Similar Reads
Python | All Permutations of a string in lexicographical order without using recursion Write a python program to print all the permutations of a string in lexicographical order. Examples: Input : python Output : hnopty hnopyt hnotpy hnotyp hnoypt ...... ytpnho ytpnoh ytpohn ytponh Input : xyz Output : xyz xzy yxz yzx zxy zyx Method 1: Using the default library itertools function permu
2 min read
Python - Ways to sort list of strings in case-insensitive manner Sorting strings in a case-insensitive manner ensures that uppercase and lowercase letters are treated equally during comparison. To do this we can use multiple methods like sorted(), str.casefold(), str.lower() and many more. For example we are given a list of string s = ["banana", "Apple", "cherry"
2 min read
Python - Ways to sort list of strings in case-insensitive manner Sorting strings in a case-insensitive manner ensures that uppercase and lowercase letters are treated equally during comparison. To do this we can use multiple methods like sorted(), str.casefold(), str.lower() and many more. For example we are given a list of string s = ["banana", "Apple", "cherry"
2 min read
Python - Ways to sort list of strings in case-insensitive manner Sorting strings in a case-insensitive manner ensures that uppercase and lowercase letters are treated equally during comparison. To do this we can use multiple methods like sorted(), str.casefold(), str.lower() and many more. For example we are given a list of string s = ["banana", "Apple", "cherry"
2 min read
Python - Sort words of sentence in ascending order Sorting words in a sentence in ascending order can be useful for tasks like text analysis, data preprocessing, or even fun applications like creating word puzzles. Itâs simple to achieve this using Python. In this article, we will explore different methods to do this.Using sorted() with SplitThe mos
3 min read
Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
3 min read