Sort List of Lists by Lexicographic Value and then Length - Python
Last Updated :
11 Jul, 2025
In this problem, sorting a list of lists by lexicographic value and then length means arranging the sublists first by their natural order (lexicographically) and then by their size. For example: For the list [[3, 2], [1, 4, 6], [1, 2], [3, 2, 1]], sorting by lexicographic value and then by length would yield [[1, 2], [3, 2], [1, 4, 6], [3, 2, 1]].
Using sort() Twice
This method first sorts the lists lexicographically and then by length. sort() function is called twice: the first to handle lexicographic sorting and the second to sort by the list length.
Python
li = [[3, 2], [1, 4, 6], [1, 2], [3, 2, 1]]
li.sort() # lexicographic sort
li.sort(key=len) # length-based sort
print(li)
Output[[1, 2], [3, 2], [1, 4, 6], [3, 2, 1]]
Using Lambda Function
This method optimizes the sorting process by calling the sorted() function only once then lambda function is used as the key combining the sorting criteria of length and lexicographic order in a single step.
Python
li = [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
# sorting using lambda function
res = sorted(li, key=lambda x: (len(x), x))
print(res)
Output[[5, 4, 1], [1, 4, 3, 2], [1, 4, 6, 7]]
Explanation: The key in sorted() uses a lambda function that generates a tuple (len(x), x) for each sublist x. This ensures sorting by length first and then lexicographically if lengths are the same.
Using cmp_to_key from functools
Instead of sorting directly with a key function we use cmp_to_key() to define a custom sorting behavior that sorts lists first by length and then lexicographically.
Python
from functools import cmp_to_key
li = [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
# Sorting using cmp_to_key, first by length, then lexicographically
res = sorted(li, key=cmp_to_key(lambda a, b: (len(a) - len(b)) or (-1 if a < b else (1 if a > b else 0))))
print(res)
Output[[5, 4, 1], [1, 4, 3, 2], [1, 4, 6, 7]]
Explanation:
- lambda function works by first comparing the lengths of a and b and if they are different it sorts based on length.
- If the lengths are equal then it compares the lists lexicographically using these notations: -1 (less than), 1 (greater than), or 0 (equal).
Using Heap Sort
This method uses heap data structure to sort the list of lists in which we first convert the lists into a heap and then extract the minimum element one by one, ensuring they are sorted by length and lexicographically.
Python
import heapq
li = [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
# Convert the list into a heap and sort
heapq.heapify(li)
res = [heapq.heappop(li) for _ in range(len(li))]
print(res)
Output[[1, 4, 3, 2], [1, 4, 6, 7], [5, 4, 1]]
Explanation
- heapq.heapify() transforms the list into a heap ensuring it can be accessed in a sorted order.
- heappop() retrieves the smallest elements based on the sorting rules which ensures the correct order.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice