Tom Tom - 1 year ago 86
Python Question

Python: How to not print comma in last element in a for loop?

My code iterates over a set and print actor's names:

for actor in actorsByMovies():
print actor+",",

The result looks like:

Brad Pitt, George Clooney,

But I want it to detect the last element so that it won't print the last comma. The result should be instead:

Brad Pitt, George Clooney

How can I do that?

Answer Source
print ', '.join(actorsByMovies())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download