Open In App

Sort Tuple of Lists in Python

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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])
After sorting based on the first element of each list, the expected output should be:
Output: ([1, 2, 5], [1, 5, 7], [5, 5, 6])

Sort a Tuple of Lists in Python

Below are some of the ways by which we can sort a tuple of lists in Python:

  • Using List Comprehension and Sorted Function
  • Using Map and Sorted Function
  • Using List Comprehension with a Lambda Function
  • Using a Custom Function

Sort a Tuple of Lists Using List Comprehension

In this approach, list comprehension is used to create a new tuple where each inner list is sorted using the sorted() function.

Python3




# Define a tuple of lists
tuple_of_lists = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
 
# Sort each inner list and create a new tuple
sorted_tuple = tuple(sorted(sublist) for sublist in tuple_of_lists)
 
print(sorted_tuple)


Output

([1, 2, 5], [1, 5, 7], [5, 5, 6])

Python Sort Tuple of Lists Using Map and Sorted Function

In this approach, we are using map to apply the sorted function to each inner list, resulting in a new tuple of sorted lists.

Python3




# Define a tuple of lists
tuple_of_lists = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
 
# Use map to apply the sorted function to each inner list and create a new tuple
sorted_tuple = tuple(map(sorted, tuple_of_lists))
 
print(sorted_tuple)


Output

([1, 2, 5], [1, 5, 7], [5, 5, 6])

Sort a Tuple of Lists Using Lambda Function

In 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




# Define a tuple of lists
tuple_of_lists = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
 
# Sort each inner list using a lambda function and create a new tuple
sorted_tuple = tuple([*map(int, sorted(sublist))]
                     for sublist in tuple_of_lists)
 
print(sorted_tuple)


Output

([1, 2, 5], [1, 5, 7], [5, 5, 6])

Python Sort Tuple of Lists Using a Custom Function

In 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




# Define a tuple of lists
tuple_of_lists = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
 
# Define a custom function to sort each inner list
def sort_inner_list(inner_list):
    return sorted(inner_list)
 
# Sort each inner list using the custom function and create a new tuple
sorted_tuple = tuple(map(sort_inner_list, tuple_of_lists))
 
print(sorted_tuple)


Output

([1, 2, 5], [1, 5, 7], [5, 5, 6])

Conclusion

In 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads