0

I have a Python script that is saving reddit posts and the first 40 top level comments from each post. The posts themselves and the comments are saved in two different dictionaries.

In the post dict, I am saving the index value for each post starting with 500. I would also like to save the index value for each comment on each post. In other words, every comment for each post should have the save index value.

In this case, each comment for the first post will have the index value of 500. Each comment for the second post will have the index value of 501. Each comment for the third post will have the index value of 502. etc. etc.

The index value works fine for the posts. The issue is with comments. Right now, post 1 comment one gets index 500, post 1 comment two gets index 501, post 1 comment 3 gets index 502 etc. etc. All comments on post 1 need to have index 500. All comments on post 2 need index 501 etc. etc.

index = 500
for submission in top_subreddit:
    index +=1
    topics_dict["title"].append(submission.title)
    topics_dict["score"].append(submission.score)
    topics_dict["id"].append(index)
    topics_dict["url"].append(submission.url)
    topics_dict["created"].append(submission.created)
    topics_dict["body"].append(submission.selftext)

    comments = submission.comments[:40]

    for comment in comments:
        if isinstance(comment, MoreComments):
            continue
        comments_dict["commentid"].append(index)
        comments_dict["commentbody"].append(comment.body)
4
  • 1
    Isn't your code already correct? You say you want to use the same index value for each comment, and it looks like you're doing so. What's the problem? You're saving it in a kind of weird way (a bunch of parallel lists), but it seems to be what you want. I'd suggest reading about enumerate (to write Pythonic loops and avoid manually managing your index value), but aside from that... Commented Jul 29, 2019 at 16:15
  • No, it's not working correctly. Right now, post 1 comment one gets index 500, post 1 comment two gets index 501, post 1 comment 3 gets index 503 etc. etc. All comments on post 1 need to have index 500. All comments on post 2 need index 501 etc. etc.
    – user715564
    Commented Jul 29, 2019 at 16:19
  • While your code already looks correct, i would advise you to use comments_dict[index].append(comment.body) instead of two different lists in one dictionary
    – Saritus
    Commented Jul 29, 2019 at 16:19
  • @user715564: Then either your indentation is wrong (differs between post and your actual code), or you left out relevant code. You only increment index in one place, once per submission; you don't show any way of incrementing index otherwise. Commented Jul 29, 2019 at 16:21

1 Answer 1

1

Avoid external vars for index number. Use it only if you're doing some manipulation with it

just go for enumerate


for apples_index, apple in enumerate(apples):
    for mangoes_index, mango in enumerate(mangoes):
        make_juice(apples_index, mangoes_index)
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.