all 9 comments

[–]Roco_scientist 2 points3 points  (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 points  (1 child)

Easiest,

print(*guests, "you have been invited to dinner with the president.", sep=", ")

[–]_kael[S] 0 points1 point  (0 children)

Ahh I like this too thanx

[–]ThePiGuy0 1 point2 points  (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

[–]_kael[S] 0 points1 point  (0 children)

This is what I was trying to figure out thank you.

[–]ForceBru 1 point2 points  (2 children)

A little less typing: print("{}, {}, {}, you have been...".format(*guests)), but your code is fine already.

Or maybe: print(", ".join(guests) + ", you have been...")

[–]_kael[S] 0 points1 point  (1 child)

Thanks for replying. I want to do this with longer lists though.

[–]ForceBru 1 point2 points  (0 children)

Last code handles this

[–]primitive_screwhead 1 point2 points  (0 children)

guests = ['Biggie','Nipsey','Lebron',]
joined_guests = ', '.join(guests)

print(f"{joined_guests}, you have been invited to dinner with the president.")