Python | Sort list of lists by lexicographic value and then length

There are many times different types of sorting has been discussed in python lists. The sorting of python list of lists has also been discussed. But sometimes, we have two parameters upon which we need to sort. First one being the list sum and next being its length. Let’s discuss how this type of problem can be solved.

Method #1 : Using sort() twice
The first approach that comes into the mind is the generic way that is to use the sort function twice, firstly on basis of the value and then on basis of size of list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# sort list of lists by value and length
# using sort() twice 
  
# initializing list 
test_list = [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
  
# printing the original list
print ("The original list is : " + str(test_list))
  
# using sort() twice 
# sort list of lists by value and length
test_list.sort()
test_list.sort(key = len)
  
# printing result
print ("The list after sorting by value and length " + str(test_list))

chevron_right


Output:



The original list is : [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
The list after sorting by value and length [[5, 4, 1], [1, 4, 3, 2], [1, 4, 6, 7]]

 
Method #2 : Using lambda function
The above method calls a single sort function twice but as an improvement to it, this method calls the sort function just once and uses lambda function to perform both sorts in one go.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# sort list of lists by value and length
# using lambda function
  
# initializing list 
test_list = [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
  
# printing the original list
print ("The original list is : " + str(test_list))
  
# using lambda function 
# sort list of lists by value and length
res = sorted(test_list, key = lambda i: (len(i), i))
  
# printing result
print ("The list after sorting by value and length " + str(res))

chevron_right


Output:

The original list is : [[1, 4, 3, 2], [5, 4, 1], [1, 4, 6, 7]]
The list after sorting by value and length [[5, 4, 1], [1, 4, 3, 2], [1, 4, 6, 7]]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.