FábioMartinho FábioMartinho - 8 months ago 32
Python Question

Python sorting a list of lists ascending with 0 at the last place

Having, in Python 2.x

x = [
[0,0,0],
[1,2,3],
[150,400,200],
[0,0,0],
[225,100,150]
]


What can I do to make it so x becomes

[
[1,2,3],
[225,100,150],
[150,400,200],
[0,0,0],
[0,0,0],
]


ordered by
x[i][2]
ascending, with 0 last?

Answer Source

This could be it:

sorted(t, key=lambda x: x[2] if x[2] > 0 else float('inf'))