# Python | Sorting list of lists with similar list elements

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 it’s 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 shorthand of the longer process that can be applied. The list is iterated and subsequent sublist is sorted using the sorted function sorting the inner list as well.

 `# 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]]]

Method #2 : Using `map() + sorted()`
The combination of above functions also perform the similar task as the above method, just the difference being that map function is used to extend the sort logic to whole of sublists.

 `# 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]]]

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.