Python Forum

Full Version: Undo interation to make a single list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using "praw" to pull reddit topic posts in a specific subreddit and here's my code:

for submission in reddit.subreddit('finance').hot(limit=50):
	titles = submission.title.replace("\n", " ")
	print(titles)
However, the return I am getting puts each post into an iterated format like below:

post 1
post 2
post 3
post 4
etc.


I would like to put it in a format all on 1 line separated by commas, or perhaps just into 1 single list separated by specific words like this:

[post, 1, post, 2, post, 3, post, 4]
You're printing each one separately. Instead, you can append them to a list.

title_list = []
for submission in reddit.subreddit('finance').hot(limit=50):
    titles = submission.title.replace("\n", " ")
    title_list.append(titles)
print(title_list)
# or....
print(",".join(title_list))
(Nov-28-2020, 09:43 PM)bowlofred Wrote: [ -> ]You're printing each one separately. Instead, you can append them to a list.

title_list = []
for submission in reddit.subreddit('finance').hot(limit=50):
    titles = submission.title.replace("\n", " ")
    title_list.append(titles)
print(title_list)
# or....
print(",".join(title_list))


Thank you, that works!