Sorting lists in Python (version 3!)

Sorting a 1-dimensional list is easy.

     lst = [4,9,1,3,2]
     lst.sort()
results in lst = [1,2,3,4,9] Note that the old contents of lst are overwritten.

If you need another copy of the list in the new order, you do not want the old list to be modified, use the function (note, not method) sorted()

     lst = [4,9,1,3,2]
     newlst = sorted(lst)
results in newlst = [1,2,3,4,9] and lst = [4,9,1,3,2]

For 2-dimensional lists (lists of lists) the method and function work but they always sort first by the first column, then if needed the second column, and so on.

     lst = [['c', 13], ['b', 10], ['a', 23], ['b', 8]]
     lst.sort()
gives lst = [['a', 23], ['b', 8], ['b', 10], ['c', 13]] Note that the elements are in order by the letter column, and when there were duplicates (the 'b's) the second element in the row was used to decide which element comes first (8 comes before 10)

If you want to sort a 2-dimensional list but NOT using the first column as the order, here is some code that will do it.

from random import randint

# Demonstrates sorting 2-dimensional lists by any column
#  the get_item function should return the element in the desired column

# the only purpose of this function is to return the 2nd element in the
#  1-dimensional list that is the parameter
def get_item(lst):
    return lst[1]


def main():
    # building a list of 8 rows which have 2 random numbers each
    mylst = []
    for i in range(8):
        mylst.append( [randint(10,50), randint(100, 200)] )
        
    print("Original list", mylst)
    print("\nSorted by first column, ascending order", sorted(mylst) ) # sorted by first column
    print("\nSorted by first column, descending order", sorted(mylst, reverse = True))
    print("\n\nSorted by second column, ascending order", sorted(mylst, key=get_item))
    print("\nSorted by second column, descending order", sorted(mylst, key=get_item, reverse = True))
    print("\nOriginal list unchanged", mylst)
main()

Note there is a little function get_item whose only purpose is to return the desired element from one row of the list being sorted.
Note the new syntax, "key=get_item". This is a place where you are providing the NAME of the function, not calling the function. So there are no parentheses after get_item. The sort method or sorted function will call the given function as needed.

Original list [[35, 182], [50, 141], [42, 103], [20, 176], [45, 145], [25, 107], [38, 125], [49, 105]]

Sorted by first column, ascending order [[20, 176], [25, 107], [35, 182], [38, 125], [42, 103], [45, 145], [49, 105], [50, 141]]

Sorted by first column, descending order [[50, 141], [49, 105], [45, 145], [42, 103], [38, 125], [35, 182], [25, 107], [20, 176]]


Sorted by second column, ascending order [[42, 103], [49, 105], [25, 107], [38, 125], [50, 141], [45, 145], [20, 176], [35, 182]]

Sorted by second column, descending order [[35, 182], [20, 176], [45, 145], [50, 141], [38, 125], [25, 107], [49, 105], [42, 103]]

Original list unchanged [[35, 182], [50, 141], [42, 103], [20, 176], [45, 145], [25, 107], [38, 125], [49, 105]]
This is in contrast to the older way of accomplishing this, which is just to make sure that the values you want to sort by are in the first column!