Open In App

Python | Sorting list of lists with similar list elements

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Let’s discuss certain ways in which this can be performed.

Method #1 : Using sorted() + list comprehension 

In this method, we just use the shorthand of the long process that can be applied. The list is iterated and the subsequent sublist is sorted using the sorted function sorting the inner list as well. 

Step bt step approach:

  1. We start by initializing a nested list test_list containing two sublists, each containing a different number of lists, each of which contains two integers.
  2. We then print the original list using the print() function and the str() function to convert the list to a string for printing purposes.
  3. We use a list comprehension with the sorted() function to sort each sublist within the test_list based on the elements within each sublist.
  4. We assign the resulting sorted list of lists to the variable res.
  5. Finally, we print the resulting sorted list res using the print() function and the str() function to convert the list to a string for printing purposes.

Python3




# Python3 code to demonstrate
# Sorting list of lists with similar list elements
# using list comprehension + sorted()
 
# initializing list
test_list = [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + sorted()
# Sorting list of lists with similar list elements
res = [sorted(idx) for idx in test_list]
 
# print result
print("The list after performing sort operation : " + str(res))


Output : 

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[1, 1], [4, 4]], [[2, 2], [3, 3], [5, 5]]]

Time Complexity: O(nlogn)
Auxiliary Space: O(1)

Method #2: Using map() + sorted() 

The combination of the above functions also performs a similar task as the above method, just the difference being that map function is used to extend the sort logic to the whole of sublists. 

Python3




# Python3 code to demonstrate
# Sorting list of lists with similar list elements
# using map() + sorted()
 
# initializing list
test_list = [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + sorted()
# Sorting list of lists with similar list elements
res = list(map(sorted, test_list))
 
# print result
print("The list after performing sort operation : " + str(res))


Output : 

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[1, 1], [4, 4]], [[2, 2], [3, 3], [5, 5]]]

Time Complexity: O(n*nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 3:  Using the lambda function with the sort() method.

Approach:

  1. Initialize the list test_list with sample data.
  2. Print the original list using the print() function.
  3. Use the sort() method to sort the list of lists. The key parameter is set to a lambda function that sorts each list element.
  4. Print the sorted list using the print() function.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Sorting list of lists with similar list elements
# using lambda function and sort()
 
# initializing list
test_list = [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using lambda function and sort()
# Sorting list of lists with similar list elements
test_list.sort(key=lambda x: sorted(x))
 
# print result
print("The list after performing sort operation : " + str(test_list))


Output

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]

Time complexity: O(n log n), where n is the number of elements in the list.
Auxiliary space: O(1), as it does not use any extra space apart from the input list.

Method #4: Using functools.cmp_to_key()

  1. Import the functools module.
  2. Define a comparison function that takes two sublists as arguments and returns -1, 0, or 1 depending on their relative order.
  3. Use the sorted() function and the cmp_to_key() function from functools to sort the original list using the comparison function.

Python3




# Python3 code to demonstrate
# Sorting list of lists with similar list elements
# using functools.cmp_to_key()
 
# import functools module
import functools
 
# initializing list
test_list = [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
 
# printing original list
print("The original list : " + str(test_list))
 
# define comparison function
 
 
def compare_lists(list1, list2):
    if sorted(list1) < sorted(list2):
        return -1
    elif sorted(list1) > sorted(list2):
        return 1
    else:
        return 0
 
 
# using functools.cmp_to_key() and sorted()
# Sorting list of lists with similar list elements
test_list.sort(key=functools.cmp_to_key(compare_lists))
 
# print result
print("The list after performing sort operation : " + str(test_list))


Output

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]
The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]

Time complexity: O(N * M * log(M)), where N is the number of sublists and M is the length of the longest sublist.
Auxiliary space: O(M), where M is the length of the longest sublist, for the sorting operation.



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads