Python Program to Sort the list according to the column using lambda

Given a list, the task is to sort the list according to the column using the lambda approach.

Examples:

Input :
array = [[1, 3, 3], [2, 1, 2], [3, 2, 1]]
Output :
Sorted array specific to column 0, [[1, 3, 3], [2, 1, 2], [3, 2, 1]]
Sorted array specific to column 1, [[2, 1, 2], [3, 2, 1], [1, 3, 3]]
Sorted array specific to column 2, [[3, 2, 1], [2, 1, 2], [1, 3, 3]]

Input :
array = [[‘java’, 1995], [‘c++’, 1983], [‘python’, 1989]]
Output :
Sorted array specific to column 0, [[‘c++’, 1983], [‘java’, 1995], [‘python’, 1989]]
Sorted array specific to column 1, [[‘c++’, 1983], [‘python’, 1989], [‘java’, 1995]]

Approach:



  • sorted() built-in function in Python gives a new sorted list from an iterable.
  • key parameter to specify a function to be called on each list element prior to making comparisons.
  • lambda is used as a function to iterate on each element.
  • key = lambda x:x[i] here i is the column on which respect to sort the whole list.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to sorting list 
# according to the column
  
# sortarray function is defined
def sortarray(array):
      
    for i in range(len(array[0])):
          
        # sorting array in ascending 
        # order specific to column i,
        # here i is the column index
        sortedcolumn = sorted(array, key = lambda x:x[i])
          
        # After sorting array Column 1
        print("Sorted array specific to column {}, \
        {}".format(i, sortedcolumn))
      
# Driver code 
if __name__ == '__main__'
      
    # array of size 3 X 2 
    array = [['java', 1995], ['c++', 1983],
             ['python', 1989]]
      
    # passing array in sortarray function
    sortarray(array)

chevron_right


Output:

Sorted array specific to column 0, [[‘c++’, 1983], [‘java’, 1995], [‘python’, 1989]]
Sorted array specific to column 1, [[‘c++’, 1983], [‘python’, 1989], [‘java’, 1995]]

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.