Python Forum
Undo interation to make a single list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Undo interation to make a single list?
#1
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]
buran write Nov-28-2020, 09:12 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
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))
DustinKlent likes this post
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make Groups with the List Elements quest 2 230 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Parse String between 2 Delimiters and add as single list items lastyle 5 620 Apr-11-2021, 11:03 PM
Last Post: lastyle
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 569 Jan-24-2021, 05:24 AM
Last Post: buran
  convert List with dictionaries to a single dictionary iamaghost 3 637 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 585 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to make global list inside function CHANKC 6 793 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  How do i make a new lists out of an list ozezn1 1 477 Oct-28-2020, 10:19 PM
Last Post: Gribouillis
  Make list of dates between today back to n days Mekala 3 619 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  How to make a list of values from a dictionary list? faryad13 2 658 Sep-03-2020, 03:45 PM
Last Post: faryad13
  List index out of range error when attempting to make a basic shift code djwilson0495 4 905 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020