Guided Project: Exploring Hacker 7/8

Screen Link: https://app.dataquest.io/m/356/guided-project%3A-exploring-hacker-news-posts/7/sorting-and-printing-values-from-a-list-of-lists

Your Code: ```from operator import itemgetter, attrgetter

swap_avg_by_hour =

for hr in avg_by_hr:
hour = hr[0]
avg = hr[1]
swap_avg_by_hour.append([avg,hour])

sorted_swap = swap_avg_by_hour.sort(key=itemgetter(0), reverse=True)
print(sorted_swap)```

What I expected to happen: Sort the list of list by the first column

What actually happened: it simply prints “none”

Other details:

Hi @greg5802,

Welcome to the community! We would be happy to help you. In order to help you in the best way, we would need to check your jupyter notebook. Can you please upload the .ipynb file?

Thanks,
Sahil

Hey greg5802,

I think you can try this out:

from operator import itemgetter

sorted_swap = sorted(swap_avg_by_hour, key=itemgetter(0), reverse=True)
print(sorted_swap)

Hi Sahil,

Thanks for your vert useful posts

Can you help me please? I am stuck on the sorted part and the rest Hacker post step 7.ipynb (22.0 KB)

Click here to view the jupyter notebook file in a new tab

1 Like

Hi @frenchhadhoud,

I have slightly modified your code so that it returns a list of lists that matches the output produced by the solution code:

swap_avg_by_hour=[]
for row in avg_by_hour:
    first_e=row[0]
    second_e=row[1]
    swap_avg_by_hour.append([second_e, first_e])

print(swap_avg_by_hour)

Now, the reason you are getting an error on this line

sorted_swap = sorted(swap_avg_by_hour, key=itemgetter(0), reverse=True)

is that, you have not imported the itemgetter function. To import it, please run:

from operator import itemgetter

Best,
Sahil

1 Like

Thanks very much Sahil for your useful and prompt response

I am posting my project here maybe someone else might find it useful

Please feel free to comment on it, Sahil. You can be brutal and tell me everything you don’t like. No problem. I really need to up my game

2 Hacker News posts.ipynb (24.3 KB)

Click here to view the jupyter notebook file in a new tab

1 Like

Hi @frenchhadhoud,

This looks good! The only thing that seems to be missing is a conclusion at the end. You can add what you learned from the analysis as a conclusion.

Best,
Sahil

1 Like

Thanks a lot, Sahil

Appreciated

1 Like

A post was split to a new topic: Why do I need to assign row[0] and row[1] to variables?