-1

So I have a list of lists

[
   ["c",2,5],
   ["bb",6,2],
   ["a",7,11],
   ...
]

I want it sorted like the following :

[
   ["a",7,11],
   ["bb",6,2],
   ["c",2,5],       
   ...
]

Sorting by each lists first value. How would this be done? (So it was sorted by a < bb < c)

test acc
  • 561
  • 2
  • 11
  • 24
  • 1
    possible duplicate of: https://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples – Dani Mesejo Sep 19 '18 at 20:14
  • 1
    Possible duplicate of [How to sort (list/tuple) of lists/tuples?](https://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples) – Patrick Artner Sep 19 '18 at 20:15
  • I don't agree with the proposed dupe target. I asks about sorting based on the second element of the sublists, and interpolating from the accepted answer might leave OP with the impression that they would have to use `lambda sub: sub[0]` as the key. – timgeb Sep 19 '18 at 20:17
  • using `s = sorted([["c",2,5], ["bb",6,2], ["a",7,11],],key=lambda x:x[0])` according to the dupe works as well - it is just not needed here, as the default behaviour is sorting based in 0th index. Your answer is valid and valuable, the dupe is the "more general" answer to this question. I do not think we need 10 questions about how to sort by the 1st, 2nd, 3rd .... 10th element - nor do we need 10 canonical answers to that. – Patrick Artner Sep 19 '18 at 20:30

1 Answers1

4

list.sort and sorted already sort lexicographically. Or in other words, all you have to do is issue yourlist.sort() or result = sorted(yourlist).

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Won't this sort by 1st value, then the 2nd, then the 3rd though? – Chris_Rands Sep 19 '18 at 20:26
  • @Chris_Rands yes, so? – timgeb Sep 19 '18 at 20:27
  • Well that's not the same as sorting by only the 1st value, e.g. `sorted([["a",2], ["a", 1]])` vs `sorted([["a",2], ["a", 1]], key=lambda x: x[0])` (may not be relevant for the OP's example of course) – Chris_Rands Sep 19 '18 at 20:30
  • @Chris_Rands I don't see how that is relevant here. The sublists in both outputs are clearly sorted by their first value. For the first result, they also happen to be sorted by the second value, but that was not a constraint on the question. – timgeb Sep 19 '18 at 20:33
  • well they say "Sorting by each lists first value", there is no implied extra sorting, but I guess we do disagree – Chris_Rands Sep 19 '18 at 20:35
  • @Chris_Rands Now, an interesting question we could investigate is whether `key=lambda x: x[0]` is a performance optimization if you anticipate lots of lists that share their first value(s), or if the overhead of a function call makes the redundant comparisons faster in the end. – timgeb Sep 19 '18 at 20:35
  • yes I don't know about performance but `operator.itemgetter(0)` would likely be faster than using `lambda` anyway – Chris_Rands Sep 19 '18 at 20:36
  • @Chris_Rands they did not say *only* by the first value, in that case I think we would need the `key`. ;) – timgeb Sep 19 '18 at 20:36