Python - Multiple Column Sort in Tuples


In Python we have to implement a solution for sorting multiple columns in tuples. So we will solve this problem using basic Python functionalities and also with the help of the operator module.

Understanding the Problem

In the given problem we have to sort a list of tuples as per the multiple columns. Every tuple shows a row of data and we have to sort the rows as per the specific columns in a specific order. For example let’s see the below −

Input List = [(5, 8), (4, 6), (2, 5), (7, 9), (5, 3)]
Sorted List = [(2, 5), (4, 6), (5, 3), (5, 8),(7, 9)]

Logic for The Above Problem

To solve the problem we will use a sorted function in Python with the custom sorting key. So basically we will solve this problem with two approaches. In the first method we will use a lambda function as the sorting key. This function will extract the values from the specified columns for every tuple. And then we will apply a sorted function so the resulting tuple will be in the sorted form.

In the second approach we will use the itemgetter function from the operator module of Python. This itemgetter function will work as the sorting key in this approach. And then we will use a sorted function to sort the tuples.

Algorithm

  • Step 1 − First initialize the data as content.

  • Step 2 − Then we will sort the content with the help of a lambda function and a sorted function of Python. And the sorting will be based on the columns. First sort by column 0 and then sort by column 1.

  • Step 3 − For printing the Output on the console we will use a for loop.

Example

content = [
   (6, 2, 'apple'),
   (3, 2, 'banana'),
   (6, 2, 'cherry'),
   (4, 3, 'apple'),
   (3, 2, 'date'),
   (1, 2, 'orange'),
]
# Sort by column 0, then by column 1
sorted_content = sorted(content, key=lambda x: (x[0], x[1]))  
for elem in sorted_content:
   print(elem)

Output

(1, 2, 'orange')
(3, 2, 'banana')
(3, 2, 'date')
(4, 3, 'apple')
(6, 2, 'apple')
(6, 2, 'cherry')

Complexity

In the above algorithm for sorting the tuple based on multiple columns, time complexity is O(n log n), here n is the number of tuples in the content. The reason for this complexity is that we have used the sorted method which has an average complexity of O(n log n).

Algorithm - Another Approach

  • Step 1 − First we will import the itemgetter from the operator module of Python.

  • Step 2 − Initialize the data as content.

  • Step 3 − Sort the content given as input with the help of sorted and itemgetter functions of Python. Inside the itemgetter we will pass columns 0 and 1.

  • Step 4 − At last we will print the result as the sorted tuples.

Example - Another Approach

from operator import itemgetter

content = [
   (6, 2, 'apple'),
   (3, 2, 'banana'),
   (6, 2, 'cherry'),
   (4, 3, 'apple'),
   (3, 2, 'date'),
   (1, 2, 'orange'),
]
# Sort content by column 0, then by column 1
sorted_content = sorted(content, key=itemgetter(0, 1))  

for elem in sorted_content:
   print(elem)

Output

(1, 2, 'orange')
(3, 2, 'banana')
(3, 2, 'date')
(4, 3, 'apple')
(6, 2, 'apple')
(6, 2, 'cherry')

Complexity

The time complexity for multiple column sorting in tuples using itemgetter and sorted functions is O(n log n), here n is the number of tuples in the content. As we have used the built-in functions of Python which has time complexity of O(n log n).

Conclusion

We have successfully created the code for multiple column sorting in tuples using Python. We have used two approaches in which the built-in functions are being used. So in this article we have learned usage of lambda function, sorted function and itemgetter function of Python.

Updated on: 17-Oct-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements