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
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,322 May-22-2023, 10:39 PM
Last Post: ICanIBB
  help me to make my password list in python >>> Oktay34riza 0 576 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
  Make Groups with the List Elements quest 2 1,967 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,348 Apr-11-2021, 11:03 PM
Last Post: lastyle
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 2,740 Jan-24-2021, 05:24 AM
Last Post: buran
  convert List with dictionaries to a single dictionary iamaghost 3 2,860 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,324 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  How to make global list inside function CHANKC 6 3,085 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  How do i make a new lists out of an list ozezn1 1 1,690 Oct-28-2020, 10:19 PM
Last Post: Gribouillis
  Make list of dates between today back to n days Mekala 3 2,382 Oct-03-2020, 01:01 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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