Guided Project: Exploring Hacker News Posts_Ask posts as a list of lists

ask_posts = # for ask posts filtered
show_posts = # for show posts filtered
other_posts = # for the other posts filtered

for row in hn:
    title = row[1]
    if title.lower().startswith("ask hn"):
        ask_posts.append(title)
    elif title.lower().startswith("show hn"):
        show_posts.append(title)
    else:
        other_posts.append(title)

This piece of code above increments the empty lists with the titles only. Therefore the empty lists are not lists of lists (they dont have the other attributes like id, num_comments etc. that are found in the data set hn.
How can I include these other attributes in the list ask_posts and show_posts?

FYI - I edited your post so that your code is enclosed between a set of triple backticks ``` so that it is rendered in a more readable format. Also, when asking questions in the community, it’s always a good idea to include a link to the screen you’re working on so that those trying to help have a way of getting more context regarding your question. I don’t think it really matters so much with this question you’re asking but it’s a good habit just the same :smiley: You may want to check out this post for some great tips for asking technical questions in the community.

Since we are getting title from indexing row (our variable inside the for loop) we can simply append the entire row to these lists rather than just one particular element from row (namely: the element at index 1–>the title of the article). In other words, use:

    if title.lower().startswith("ask hn"):
        ask_posts.append(row) # swap title for row
    elif title.lower().startswith("show hn"):
        show_posts.append(row) # swap title for row

I’m not sure if this will impact further screens going forward or not because we might need ask_posts and show_posts to be a list of titles and not a list of lists…but this is how you would accomplish what you’re asking. Let me know if I’ve missed something or led you astray.

Happy learning!

Thank you very much for your response. I am grateful. I am new to this platform and therefore still learning the prerequisites of asking technical questions. Kindly if you don’t mind me asking, I am seeing people reference post[4] in ask_post as a list of titles in order to get the num_comments which clearly to me does not make sense. What is the reasoning here? Thanks.

Simply put my code above makes ask_post a list of titles only. In the next screen we are required to look for how many comments are in ask post using index 4, which I kindly do not understand, and I am getting an error.

1 Like

@mathmike314 Thank you so much for the insight. Its amazing how sometimes the solution is just in front of you. I managed to work it out through your help. Thanks once again.

1 Like

You are very welcome, it is my pleasure. Don’t worry too much about prerequisites for asking technical questions…they are more of a guideline rather than rules and nothing is “strictly enforced.” That said, the better you are at asking questions, the easier it is for people to answer you → win-win!

Perhaps it would be best to create a new post for this question and include a link to the lesson as well as the code you’re using and the error it generates. I did this lesson a long time ago and while I remember the overall idea, I can’t remember enough of the details to answer your question in situ.