Skip to main content
Tweeted twitter.com/StackCodeReview/status/1166681845477597184
Became Hot Network Question
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Condition in print statement for `end` argument Printing a list as "a, b, c." using Python

Source Link

Condition in print statement for `end` argument

Is there a better way to do this? Assuming very long lists, is this slow and wasteful?

flavors = ['chocolate', 'vanilla', 'caramel', 'strawberry', 'coffee']

for flavor in flavors:
    print(flavor, end = ', ' if (flavors.index(flavor) != len(flavors) -1) else '.')

Output:

chocolate, vanilla, caramel, strawberry, coffee.