Sorting a list of tuples in python according to an element index -


How can I sort a list of tuples in a particular state of int? (For no loop)

e.g.

> Div>

You can use it, its main function, and:

  & gt; & Gt; & Gt; From operator import items; & Gt; & Gt; & Gt; L = [[1,5,2], (7,1,4), (1,6,3)]> gt; & Gt; & Gt; L.sort (key = itemgetter (2))> gt; & Gt; & Gt; L [(1, 5, 2), (1, 6, 3), (7, 1, 4)]> gt; & Gt; & Gt;  

You can also use operator.itemgetter instead:

  & gt; & Gt; & Gt; L = [[1,5,2], (7,1,4), (1,6,3)]> gt; & Gt; & Gt; L.sort (key = lambda x: x [2])> gt; & Gt; & Gt; L [(1, 5, 2), (1, 6, 3), (7, 1, 4)]> gt; & Gt; & Gt;  

However, the latter is the one.


Comments