Python - Multiple Column Sort in Tuples
Last Updated :
23 Mar, 2023
Sometimes, while working with records, we can have a problem in which we need to perform sort operation on one of the columns, and on other column, if equal elements, opposite sort. This kind of problem can occur as application in many domains such as web development. Lets discuss certain ways in which this problem can be solved.
Input : test_list = [(6, 7), (6, 5), (6, 4), (7, 10)]
Output : [(7, 10), (6, 4), (6, 5), (6, 7)]
Input : test_list = [(10, 7), (8, 5)]
Output : [(10, 7), (8, 5)]
Method #1 : Using sorted() + lambda
The combination of above functions can offer one of the ways to solve this problem. In this, we perform sort using sorted() and order and column manipulation is handled by lambda function.
Python3
# Python3 code to demonstrate working of
# Multiple Column Sort in Tuples
# Using sorted() + lambda
# initializing list
test_list = [(6, 7), (6, 5), (1, 4), (8, 10)]
# printing original list
print("The original list is : " + str(test_list))
# Multiple Column Sort in Tuples
# Using sorted() + lambda
res = sorted(test_list, key = lambda sub: (-sub[0], sub[1]))
# printing result
print("The sorted records : " + str(res))
Output : The original list is : [(6, 7), (6, 5), (1, 4), (8, 10)]
The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Time Complexity: O(nlogn) where n is the number of elements in the in the list “test_list”. The sorted() + lambda is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(1) additional space is not required
Method #2 : Using itemgetter() + sorted()
This is yet another way in which this task can be performed. In this, we perform the task required for lambda function using itemgetter().
Python3
# Python3 code to demonstrate working of
# Multiple Column Sort in Tuples
# Using itemgetter() + sorted()
from operator import itemgetter
# initializing list
test_list = [(6, 7), (6, 5), (1, 4), (8, 10)]
# printing original list
print("The original list is : " + str(test_list))
# Multiple Column Sort in Tuples
# Using itemgetter() + sorted()
res = sorted(test_list, key = itemgetter(1))
res = sorted(res, key = itemgetter(0), reverse = True)
# printing result
print("The sorted records : " + str(res))
Output : The original list is : [(6, 7), (6, 5), (1, 4), (8, 10)]
The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Method #3:Using listcomprehension
- Define the original list of tuples.
- Define the key function for sorting the tuples. The key function should take a tuple as input and return a tuple of values that will be used for sorting the original list of tuples.
- Call the sorted() function with the original list of tuples and the key function as arguments.
- Store the sorted list of tuples in a variable.
- Print the sorted list of tuples.
Python3
# initializing list
test_list = [(6, 7), (6, 5), (1, 4), (8, 10)]
# printing original list
print("The original list is : " + str(test_list))
# Multiple Column Sort in Tuples
# Using list comprehension
res = sorted(test_list, key=lambda sub: (-sub[0], sub[1]))
# Alternatively, using list comprehension
res = [(x, y) for (x, y) in sorted(test_list, key=lambda sub: (-sub[0], sub[1]))]
# printing result
print("The sorted records : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : [(6, 7), (6, 5), (1, 4), (8, 10)]
The sorted records : [(8, 10), (6, 5), (6, 7), (1, 4)]
Time complexity:
Sorting the original list of tuples using sorted() takes O(N log N) time complexity, where N is the number of tuples in the list.
The lambda function takes O(1) time complexity as it performs a constant number of operations for each tuple.
Therefore, the overall time complexity for performing the multiple column sort using sorted() and lambda function is O(N log N).
Space complexity:
Sorting the original list of tuples using sorted() requires O(N) space complexity to store the sorted list of tuples.
The lambda function requires O(1) space complexity to store the tuple of values for sorting.
Therefore, the overall space complexity for performing the multiple column sort using sorted() and lambda function is O(N).
Similar Reads
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
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
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
Python | Convert Lists to column tuples
Sometimes, while working with data, we can have a problem in which we need to get alike index elements in a single container. This means the columns of Matrix/Multiple lists need to be converted to list of tuples, each comprising of like index elements. Let's discuss certain ways in which this task
11 min read
Python - Column Minimum in Tuple list
Sometimes, while working with records, we can have a problem in which we need to find min of all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using min()
6 min read
Convert List of Tuples To Multiple Lists in Python
When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
3 min read
Sort Tuple of Lists in Python
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], [
3 min read
Python - Tuple Matrix Columns Summation
Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
8 min read
Python - Add Custom Column to Tuple list
Sometimes, while working with Python records, we can have a problem in which we need to add custom column to tuples list. This kind of problem can have application in data domains such as web development. Lets discuss certain ways in which this task can be performed. Input : test_list = [(3, ), (7,
5 min read
Python | Compare tuples
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read