Reputation: 1690
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)
Upvotes: 0
Views: 537
Reputation: 902
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)
Upvotes: 1