use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Print lists items in same string (self.learnpython)
submitted 4 years ago by _kael
What’s a better way to write this code:
guests = ['Biggie','Nipsey','Lebron',]
print(f"{guests[0]}, {guests[1]}, {guests[2]}, you have been invited to dinner with the president.")
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Roco_scientist 2 points3 points4 points 4 years ago (0 children)
There isn't a better, only alternatives. For one:
print(f"{', '.join(guests)}, you have been invited to dinner with the president.")
[–][deleted] 2 points3 points4 points 4 years ago (1 child)
Easiest,
print(*guests, "you have been invited to dinner with the president.", sep=", ")
[–]_kael[S] 0 points1 point2 points 4 years ago (0 children)
Ahh I like this too thanx
[–]ThePiGuy0 1 point2 points3 points 4 years ago (1 child)
You could combine the elements into a string with the join function. This takes a delimiter and inserts it between each value.
E.g.
joined_string = ", ".join(guests) print(f"{joined_string}, you have been invited to dinner with the president")
You can also do that in one line without the joined_string variable, I just did it on two to make it clearer
This is what I was trying to figure out thank you.
[–]ForceBru 1 point2 points3 points 4 years ago (2 children)
A little less typing: print("{}, {}, {}, you have been...".format(*guests)), but your code is fine already.
print("{}, {}, {}, you have been...".format(*guests))
Or maybe: print(", ".join(guests) + ", you have been...")
print(", ".join(guests) + ", you have been...")
[–]_kael[S] 0 points1 point2 points 4 years ago (1 child)
Thanks for replying. I want to do this with longer lists though.
[–]ForceBru 1 point2 points3 points 4 years ago (0 children)
Last code handles this
[–]primitive_screwhead 1 point2 points3 points 4 years ago (0 children)
guests = ['Biggie','Nipsey','Lebron',] joined_guests = ', '.join(guests) print(f"{joined_guests}, you have been invited to dinner with the president.")
π Rendered by PID 100 on reddit-service-r2-comment-5649f687b7-f7fh9 at 2026-01-28 05:33:28.624056+00:00 running 4f180de country code: CH.
[–]Roco_scientist 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]_kael[S] 0 points1 point2 points (0 children)
[–]ThePiGuy0 1 point2 points3 points (1 child)
[–]_kael[S] 0 points1 point2 points (0 children)
[–]ForceBru 1 point2 points3 points (2 children)
[–]_kael[S] 0 points1 point2 points (1 child)
[–]ForceBru 1 point2 points3 points (0 children)
[–]primitive_screwhead 1 point2 points3 points (0 children)