# Question 20 in List Practice

My Code:

``````def sort_index(some_list):
some_list.sort(key=lambda x: x[2], reverse=True)
some_list.sort(key=lambda x: x[0])

``````

What actually happened:

``````Function  `sort_index`  did not correctly modify the value of argument number  `1` .
``````

``````def sort_index(some_list):
some_list.sort(key=lambda x: x[0])
some_list.sort(key=lambda x: x[2], reverse=True)
``````

Can anyone help me to understand why there is an error?
What is the purpose of resolves ties when we sort the list of lists?

Thanks

1 Like

If you are gonna post the answers of the practice questions directly without first taking time to review the question in case an error is encountered and also without comparing steps of your code with that of the solution, unless you have a more efficient code, then the entire purpose of giving a practice test is lost.

In this case, the question was to first sort the â€ślist of listsâ€ť by the 3rd element in descending order and then break the tie by the 1st element in ascending order.

Your code would work if the question was the other way round. I hope that helps.

1 Like

@Rucha Thanks for spending the time to reply back to me. I understand you want me to spend the time review the question before posting the answer too early. It sounds like I am being really annoying and missing the purpose of taking the practice test. I guess everyone has a different knowledge level and I do not fully understand this practice question. so please let me continue (sorry for being so annoying ):

Blockquote
In this case, the question was to first sort the â€ślist of listsâ€ť by the 3rd element in descending order and then break the tie by the 1st element in ascending order.

In my code I was running this line of code first:

``````some_list.sort(key=lambda x: x[2], reverse=True)
``````

then:

``````some_list.sort(key=lambda x: x[0])
``````

but I do not know why I should reverse the sequence as the standard answers?

FYI:
I do not recall there is a lot of content covers sort list of lists within the data analyst path. This article helped me understand sort list of lists topic and I am sharing it in the post.

Hey.

These problems specifically stand outside of the content. Theyâ€™re just challenges.

Anyway, hereâ€™s a hint straight from the Python documentation:

The highlighted part justifies the order.

Thatâ€™s a typical need when handling data: to sort by multiple columns.

2 Likes

@Bruno @Rucha Thank you for helping me out and thank you for being patient with me!

2 Likes

Asking a question or asking a detailed explanation about the difference(s) between your own code and solution is not annoying. Thatâ€™s how we learn better!

Take it this way, if your solution was not accepted it clearly indicates your code even if technically, logically (but for a different purpose), and syntactically is right, it is not giving the output asked for. time to

However, if your code output is accepted, but the code doesnâ€™t match the solution, that means you took a different approach and still got the same answer.

Then asking for an explanation about the difference/ similarities makes sense, but without revealing the solution given. Of course, you will have to post your own solution though!

This will enable other readers to make sense of the question first, think of a solution, run that, and then look for any mismatch.

And my previous post clearly reflects my stupidity as a reader!

Thanks, @Bruno for solving the actual question.

So, I still donâ€™t understand why we write down the code in reverse sequence despite the question asks vice verse sequence?