Sort Tuple of Lists in Python
Last Updated :
01 Feb, 2025
The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [5, 6, 5]).The goal is to generate a new tuple where each list inside the tuple is sorted. The resulting tuple will be ([1, 2, 5], [1, 5, 7], [5, 5, 6]) .
Using list comprehension
When sorting a tuple of lists, list comprehension provides a efficient approach. It allows us to iterate through the tuple, apply sorting to each list, and create a new tuple with the sorted lists.
Python
a = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
res = tuple([sorted(i) for i in a])
print(res)
Output([1, 2, 5], [1, 5, 7], [5, 5, 6])
Explanation: list comprehension iterate through each list in the tuple a, applying the sorted() to each list. The result is a new list of sorted lists, which is then converted back into a tuple using tuple().
Using map()
map() is another popular method for applying a function to each item in an iterable. In the case of sorting a tuple of lists, we can use map() to apply the sorted() to each list within the tuple and then convert the result back into a tuple.
Python
a = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
res = tuple(map(sorted, a))
print(res)
Output([1, 2, 5], [1, 5, 7], [5, 5, 6])
Explanation: map() applies sorted() to each list in the tuple a and tuple() converts the result into a tuple of sorted lists.
Using lambda function
Lambda functions provide a flexible way to define small, anonymous functions for specific tasks. When combined with map(), we can use a lambda function to apply sorting to each list inside the tuple.
Python
a = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
res = tuple(map(lambda x: sorted(x), a))
print(res)
Output([1, 2, 5], [1, 5, 7], [5, 5, 6])
Explanation: map() applies a lambda function to each list in the tuple a, where the lambda function sorts each list using sorted(x). The result is then converted into a tuple using tuple() .
Using for looop
While less concise, for loop gives us more control over the sorting operation. In this approach, we iterate through the tuple, sort each list and then collect the sorted lists into a new tuple.
Python
a = ([2, 1, 5], [1, 5, 7], [5, 6, 5])
res = [] # Initialize an empty list
for i in a:
res.append(sorted(i))
res = tuple(res)
print(res)
Output([1, 2, 5], [1, 5, 7], [5, 5, 6])
Explanation: for loop iterates over each list i in the tuple a and sorted(i) sorts the list. The sorted list is then appended to the list res, which will later be converted into a tuple.
Similar Reads
Python | Sort lists in tuple Sometimes, while working with Python tuples, we can have a problem in which we need to sort the tuples which constitutes of lists and we need to sort each of them. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + sorted() + generator expression This task ca
5 min read
Custom Sorting in List of Tuples - Python The task of custom sorting in a list of tuples often involves sorting based on multiple criteria. A common example is sorting by the first element in descending order and the second element in ascending order. For example, given a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)], sorting the tuples by t
3 min read
Sort a list in python Sorting is a fundamental operation in programming, allowing you to arrange data in a specific order. Here is a code snippet to give you an idea about sorting.Python# Initializing a list a = [5, 1, 5, 6] # Sort modifies the given list a.sort() print(a) b = [5, 2, 9, 6] # Sorted does not modify the gi
5 min read
Sort a List of Tuples by Second Item - Python The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)],
2 min read
Sort mixed list in Python Sometimes, while working with Python, we can have a problem in which we need to sort a particular list that has mixed data types. That it contains integers and strings and we need to sort each of them accordingly. Let's discuss certain ways in which this problem can be solved. Method #1: Using sort(
4 min read
Python - List of tuples to multiple lists Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data.Using zip()zip() function is a concise
3 min read