Can't append specific rows to new lists(Guided project:Hacker News)

Hi! When I was doing the second guided project: Hacker News, I tried to separate “Ask HN” and “Show HN” rows to different lists. But when I printed the lengths of these lists, the numbers are 0. I’m not sure if its because of the dataset or other problems. I downloaded the dataset from Kaggle.
Here is the code file:
Exploring Hacker News Posts.ipynb (4.6 KB)

Here is my code:

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

Thank you!

Hi @stellayou1126,
I think you forget to use the command reader() to read your opened file
https://app.dataquest.io/m/312/lists-and-for-loops/7/opening-a-file

from csv import reader
opened_file = open("HN_posts_year_to_Sep_26_2016.csv")
opened_file = reader(opened_file)
hn = list(opened_file)
print(hn[:5])

and olso title = row[1] not title =hn[1]

1 Like

Hi @bahmed21, thank you so much for your help!

1 Like