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
Unzip List of Tuples in Python
The task of unzipping a list of tuples in Python involves separating the elements of each tuple into individual lists, based on their positions. For example, given a list of tuples like [('a', 1), ('b', 4)], the goal is to generate two separate lists: ['a', 'b'] for the first elements and [1, 4] for
2 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
How To Slice A List Of Tuples In Python?
In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
3 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
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ].Using print()print() function is the
2 min read
Find the size of a list - Python
In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory (in bytes) occupied by a list object. In this article, we will learn various ways to get the size of a python list. 1.Using get
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