![]() |
Sorting a tuple of lists in Python involves arranging the lists within the tuple based on a specific criterion. In this article, we will learn how to sort a tuple of a list in Python. Example: Input: ([2, 1, 5], [1, 5, 7], [5, 6, 5]) Sort a Tuple of Lists in PythonBelow are some of the ways by which we can sort a tuple of lists in Python:
Sort a Tuple of Lists Using List Comprehension
Python3
Output
([1, 2, 5], [1, 5, 7], [5, 5, 6]) Python Sort Tuple of Lists Using Map and Sorted FunctionIn this approach, we are using map to apply the Python3
Output
([1, 2, 5], [1, 5, 7], [5, 5, 6]) Sort a Tuple of Lists Using Lambda FunctionIn this approach, list comprehension is used to create a new tuple. and lambda function is used within the sorted function to sort each inner list while map function is used to convert sorted values to integers. Python3
Output
([1, 2, 5], [1, 5, 7], [5, 5, 6]) Python Sort Tuple of Lists Using a Custom FunctionIn this approach, a custom function (sort_inner_list) is defined to encapsulate the logic for sorting each inner list. This custom function takes an inner list as an argument and returns the sorted version of that list. Python3
Output
([1, 2, 5], [1, 5, 7], [5, 5, 6]) ConclusionIn this article we studied how to sort a tuple of lists in Python, we’ve examined several methods, each offering unique approaches to meet different requirements. Sorting tuples based on specific criteria enhances flexibility when working with complex data structures. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |