How to Avoid Last Comma in Python Loop

How to avoid last comma in python loop

Pass the results as separate arguments and specify the separator:

 print(*result, sep=',')

How to remove the last comma when using for loop?

Take the comma off the end of the string. Then add a check to see if i is less than the collection length - 1. Something like…

Nationalitiesstr = Nationalitiesstr + this.nationalityData.find(x => x.libraryID == Nationalities[i].nationalityXid).libraryName;

if (i < Nationalities.length -1)
Nationalitiesstr += “, “;

The String.Join Method … may also work.

Django loop – remove last comma

What about this?

{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>"
{%if not forloop.last%},{%endif%}
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}


Related Topics



Leave a reply



Submit