Jagmohan Singh
Jagmohan Singh

Reputation: 93

How to sort list of list on the basis of two elements, in Python?

I have a=[[1,2],[2,1],[3,2],[5,1],[4,1]]
I want to get the result sorted list as
[[2,1],[4,1],[5,1],[1,2],[3,2]]

Upvotes: 1

Views: 149

Answers (1)

Jagmohan Singh
Jagmohan Singh

Reputation: 93

I got this by doing following-
a.sort(key=lambda tup:tup[0])
a.sort(key=lambda tup:tup[1])

Upvotes: 2

Related Questions