1

How can i sort a list in python? I have this list:

list=[[[1,2,3],0],[[13,2,3],2],[[2,0,0],13]]

And i want to sort by second element from first list. I've think that should be something like this .

list.sort(key=lambda x:x[0[1]])

And I don't know the correct syntax.

2 Answers 2

2

Your indexing is wrong. You want to index the list, not the integer.

list.sort(key=lambda x: x[0][1])
Sign up to request clarification or add additional context in comments.

Comments

0

Well, its simple enough:

>>> l=[[[1,2,3],0],[[13,2,3],2],[[2,0,0],13]]
>>> sorted(l, key=lambda x: x[0][1])
[[[2, 0, 0], 13], [[1, 2, 3], 0], [[13, 2, 3], 2]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.