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?
You can use ', '.
An alternative method of removing the comma from last foreach loop item. In this alternative way, we are going to use the PHP array_push() function. below is the code: <?
Python 3 changes this completely and the trailing comma is no longer accepted. You use the end parameter to change the line ending, setting it to a blank string to get the same effect. More like a necessary consequence.
print(', '.join(actorsByMovies()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With