nidHi
nidHi

Reputation: 833

Sorting list of lists with depth more than 2

I have the list a list of the following type:

x = [ [[5,"j"],[1,3,4]] , [[5,"a"],[2,0,8]] , [[5,"d"],[3,0,2]] , [[5,"c"],[8,7,6]] ]

I want to sort this list based on the element x[i][0][1] ( i being any integer within the length of the list). I have been trying to use itemgetter() and x.sort(key=lambda x:x[0]). I tried different variations using these two ways but am not getting the expected output and would need some guidance on the same.

Upvotes: 0

Views: 198

Answers (1)

ettanany
ettanany

Reputation: 19816

Based on your explanation, to do that, you can use sorted with key argument like below:

x = [ [[5,"j"], [1,3,4]] , [[5,"a"], [2,0,8]] , [[5,"d"], [3,0,2]] , [[5,"c"], [8,7,6]] ]

sorted_list = sorted(x, key=lambda item: item[0][1])

Upvotes: 3

Related Questions