16
c2=[]
row1=[1,22,53]
row2=[14,25,46]
row3=[7,8,9]

c2.append(row2)
c2.append(row1)
c2.append(row3)

c2 is now:

[[14, 25, 46], [1, 22, 53], [7, 8, 9]]

how do i sort c2 in such a way that for example:

for row in c2:

sort on row[2]

the result would be:

[[7,8,9],[14,25,46],[1,22,53]]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • You're going to have to be a bit more specific as to how exactly you want your stuff sorted. I see the order of the elements in the inner lists stays the same, but the order of the lists themselves changes. Explain your logic. – Matti Virkkunen Aug 03 '10 at 16:33
  • im sorry, i had a mistake in the data, check it now – Alex Gordon Aug 03 '10 at 16:34

3 Answers3

26

The key argument to sort specifies a function of one argument that is used to extract a comparison key from each list element. So we can create a simple lambda that returns the last element from each row to be used in the sort:

c2.sort(key = lambda row: row[2])

A lambda is a simple anonymous function. It's handy when you want to create a simple single use function like this. The equivalent code not using a lambda would be:

def sort_key(row):
    return row[2]

c2.sort(key = sort_key)

If you want to sort on more entries, just make the key function return a tuple containing the values you wish to sort on in order of importance. For example:

c2.sort(key = lambda row: (row[2],row[1]))

or:

c2.sort(key = lambda row: (row[2],row[1],row[0]))
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • 1
    @I__: It's used to define quick inline functions. http://www.secnetix.de/olli/Python/lambda_functions.hawk – Matti Virkkunen Aug 03 '10 at 16:38
  • thank you! the other question is how do i first sort by row[2] and within that set by row[1] – Alex Gordon Aug 03 '10 at 16:46
  • You could also do something like `import operator; c2.sort(key=operator.itemgetter(2))` or `c2.sort(key=operator.itemgetter(2, 1))` or `c2.sort(key=operator.itemgetter(2, 1, 0))`. It's apparently a fair bit faster than using a lambda expression. – JAB Aug 03 '10 at 19:29
4
>>> import operator
>>> c2 = [[14, 25, 46], [1, 22, 53], [7, 8, 9]]
>>> c2.sort(key=itemgetter(2))
>>> c2
[[7, 8, 9], [14, 25, 46], [1, 22, 53]]
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
3

Well, your desired example seems to indicate that you want to sort by the last index in the list, which could be done with this:

sorted_c2 = sorted(c2, lambda l1, l2: l1[-1] - l2[-1])
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • the other question is how do i first sort by row[2] and within that set by row[1] – Alex Gordon Aug 03 '10 at 16:46
  • mipadi, im sorry can u explain your answer, it seems very complicated – Alex Gordon Aug 03 '10 at 16:46
  • Both are fine. mipdadi is using an alternative option to me. Instead of specifying a `key` function which returns a single value to sort on, he's using a `sort` function which takes two values, compares then and returns an appropriate value. I'd argue that my option is easier though. :-) – David Webb Aug 03 '10 at 16:47
  • great! then he can help me sort on two elements – Alex Gordon Aug 03 '10 at 16:47
  • It should be noted too that whenever you can sort with the `key` option, you should. It will in general be faster than the pure comparison-based sort. – Carlos Scheidegger Aug 03 '10 at 17:04
  • the `cmp` argument of sort is deprecated in Python2 and has been removed from Python3. You should use the `key` argument instead as it is more efficient – John La Rooy Aug 03 '10 at 17:15