3

I have a list of lists that looks like this

l1 = [[1,'steve'],[4,'jane'],[3,'frank'],[2,'kim']]

and I want to order them by the first number

how would i go about this, thank you

  • 1
    `.sort` orders by the first element by default so you should be able to say `l1.sort()` – Hoopdady Nov 22 '16 at 14:24
  • 3
    Possible duplicate of [How to sort a list of lists by a specific index of the inner list?](http://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list) – Stormvirux Nov 22 '16 at 14:24
  • 2
    Possible duplicate of [Python sort() first element of list](http://stackoverflow.com/questions/21068315/python-sort-first-element-of-list) – amin Nov 22 '16 at 14:25

6 Answers6

11

By default, sorted will use the first element:

>>> l1 = [[1,'steve'],[4,'jane'],[3,'frank'],[2,'kim']]
>>> sorted(l1)
[[1, 'steve'], [2, 'kim'], [3, 'frank'], [4, 'jane']]

As noted in the comments, l1.sort() will sort the original list in place, and is a better option if you don't need to preserve the original order.

This is the way the comparison operators work in Python:

>>> [1, 'something'] < [2, 'something']
True

Note that if the first element compares as equal, it will compare the second element, and so on:

>>> [1, 1, 1] < [1, 1, 2]
True

This means that two names with the same number would be sorted (lexicographically) by the string, not in place:

>>> sorted([[1, 'mike'], [1, 'bob']])
[[1, 'bob'], [1, 'mike']]
brianpck
  • 8,084
  • 1
  • 22
  • 33
  • 2
    It actually uses every element if needed. If 2 elements have the same first element, sorted will compare the 2nd element. see `[[1, 2], [4, 1], [3, 2], [3, 1], [2, 0]]` – Eric Duminil Nov 22 '16 at 14:29
  • 2
    It's important to note that `sorted(l1)` returns a sorted copy whereas `l1.sort()` sorts in place. Unless the OP needs th preserve the original order, I would recommend against using `sorted()` over `.sort()`. – Steven Rumbalski Nov 22 '16 at 14:40
4

Python sort will compare the lists with the first element by default. If they're the same, it will continue with the second element, and so on.

so

l1.sort()

will do.

But if you really just want to sort by first value and nothing else :

sorted(l1, key=lambda id_and_name: id_and_name[0])
#=> [[1, 'steve'], [2, 'kim'], [3, 'frank'], [4, 'jane']]
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
3
l1.sort(key=lambda x: int(x[0]))

Should do the trick make sure you have the int() cast as you can run into further errors with larger numbers because the integers are lexicographically compared. i.e., '5' will be larger than '20'

pladder
  • 142
  • 1
  • 10
  • 2
    A simple `l1.sort()` will do the trick. as far as casting to int, the example dat shows all as being int. I don't think it's needed. – Steven Rumbalski Nov 22 '16 at 14:47
0
from operator import itemgetter
l1 = [[1,'steve'],[4,'jane'],[3,'frank'],[2,'kim']]
l1 = sorted(l1, key=itemgetter(0))
blue_note
  • 27,712
  • 9
  • 72
  • 90
  • It's important to note that `sorted(l1)` returns a sorted copy whereas `l1.sort()` sorts in place. Unless the OP needs th preserve the original order, I would recommend against using `sorted()` over `.sort()`. Nice use of `itemgetter`, but a simple `l1.sort()` will do the trick. – Steven Rumbalski Nov 22 '16 at 14:43
  • Please provide some explanation why this answer solves the question. – Laurent LAPORTE Nov 22 '16 at 19:51
0
l1 = [[1,'steve'],[4,'jane'],[3,'frank'],[2,'kim']]
ol = sorted(l1, key=lambda key_value: key_value[0])

print (ol)

output:

[[1, 'steve'], [2, 'kim'], [3, 'frank'], [4, 'jane']]
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
0

Try the following:

l1 = [[1,'steve'],[4,'jane'],[3,'frank'],[2,'kim']]
sorted_l1 = sorted(l1, key=lambda item: item[0])  # Or simply sorted(l1)

Output:

>>> sorted_l1
[[1, 'steve'], [2, 'kim'], [3, 'frank'], [4, 'jane']]

If you want to update the same list (to be sorted), try the following:

>>> l1.sort(key=lambda item: item[0])  # Or simply l1.sort()
>>> l1
[[1, 'steve'], [2, 'kim'], [3, 'frank'], [4, 'jane']]
ettanany
  • 19,038
  • 9
  • 47
  • 63