Open In App

Sort a list of strings in lexicographical order in Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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().


Practice Tags :

Similar Reads