First page Back Continue Last page Overview Graphics

Another Example

Sorting of a list, which consists of 2-lists, by sorting them according to the second list element.

>>> lst=[[2,6],[1,3],[5,4],[1,0]]

>>> lst.sort(key=lambda x: x[1])

>>> lst

[[1, 0], [1, 3], [5, 4], [2, 6]]

Sort according to the sum of the components of a 2-tuple:

>>> lst=[[2,6],[1,3],[5,4],[1,0]]

>>> lst.sort(key = sum)

>>> print(lst)

[[1, 0], [1, 3], [2, 6], [5, 4]]

>>>