You say "I would like to sort this list a, so that I can get the lexicographically smallest row, column which in this case is [4,3]". But if all you're interested in is getting the smallest element of a list, then you don't need to sort it. You can just use builtin function min
.
Min row
Both sorting and taking the min use lexicographical order by default in python, so you can just call min
directly:
a = [[4,3],[4,5]]
min_row = min(a)
print(min_row)
# [4, 3]
Min column
If you want to get the min column in lexicographical order, then you need to transpose your list of lists so that the inner lists correspond to the columns. See this relevant question: Transpose list of lists and the documentation on zip
a = [[4,3],[4,5]]
min_col = min(zip(*a))
print(min_col)
# (3, 5)
Sorting
If you really want to sort rather than taking the minimum element, then you can use .sort()
or sorted( )
rather than min( )
.
a = [[4,3],[4,5]]
# sorting a copy
sorted_rows = sorted(a)
print(sorted_rows)
# sorting in-place
a.sort()
print(a)