Question 20 in List Practice

Screen Link:
https://app.dataquest.io/m/1018/lists/20/sorting-lists-of-lists-ii

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` .

The suggested answer is:

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

hi @jinyushan1990

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 :man_facepalming:):

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! :love_you_gesture:

2 Likes

hi @jinyushan1990

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 :thinking:

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. :+1:

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! :grin:

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. :slight_smile:

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