<p>While your conditional is complete personally preference I would do the following as I find it slightly easier to read. Note the use of the <code>{%- if -%}</code> to trim <a href="http://twig.sensiolabs.org/doc/templates.html#whitespace-control">whitespace</a>:</p>

<pre><code>{% for category in entry.mediaType %}
    &lt;b&gt;{{ category.title }}&lt;/b&gt;
    {%- if not loop.last -%}
      ,
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-10-04T05:18:37.447Z
To clean it up even further: `{{ not loop.last ? "," }}`
Lindsey D
CC BY-SA 3.0
2017-11-07T09:30:25.497Z
@LindseyD Your suggestion seems very neat but why do I get `unexpected char '?'` ?
Pranasas
CC BY-SA 3.0
2017-11-07T14:16:08.420Z
@Pranasas I don’t know. I suggest starting a new thread, since this one is over 3 years old. Feel free to link back to this thread (or answer) for reference.
Lindsey D
CC BY-SA 3.0
2014-10-04T04:17:50.333Z
2014-10-04T04:17:50.333Z
jnowland
<pre><code>{% for category in entry.mediaType %}
    &lt;b&gt;{{ category.title }}&lt;/b&gt;{{ "," if not loop.last }}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-12-11T09:56:02.003Z
2019-02-27T06:33:47.363Z
2019-02-27T06:33:47.363Z
Chuanwen Chen
<p>There is a special tag inside for loops you can use for this</p>

<pre><code>{% for category in entry.mediaType %}
    &lt;b&gt;{{ category.title }}&lt;/b&gt;,
    {% if loop.last %}
        &lt;b&gt;{{ category.title }}&lt;/b&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>Full list <a href="https://gist.github.com/brandonkelly/8584132#craft-2" rel="noreferrer">here</a></p>

CC BY-SA 3.0
2014-10-03T16:31:11.917Z
Awesome, I could not find this anywhere in the Craft docs, thank you!
Matt Wilcox
CC BY-SA 3.0
2014-10-03T17:10:29.200Z
You didn't find it in the Craft docs because [it's a native Twig variable](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable).
Lindsey D
CC BY-SA 3.0
2014-10-04T03:57:36.047Z
Is there an advantage to this vs wrapping the "," in a `{% if not loop.last %}`?
Dustin Walker
CC BY-SA 3.0
2014-10-04T04:12:52.533Z
Personal preference.
jnowland
CC BY-SA 3.0
2014-10-03T16:25:39.250Z
2014-10-03T16:25:39.250Z
Seán O'Grady
<p>I want to create a comma separated list of categories to which an entry belongs, but without the trailing slash on the last element... can't work out how to remove it!</p>

<pre><code>{% for category in entry.mediaType %}
    &lt;b&gt;{{ category.title }}&lt;/b&gt;,
{% endfor %}
</code></pre>

<loop>
Remove trailing comma from an output loop?
# Remove trailing comma from an output loop?

(Asked by: username_0 on 2014-10-03)

I want to create a comma separated list of categories to which an entry belongs, but without the trailing slash on the last element... can't work out how to remove it!
{% for category in entry.mediaType %}
    {{ category.title }},
{% endfor %}

**Tags:** loop

---

## 3 answers

### Answer by username_5 on 2014-10-03. Score: 15 | Accepted answer

There is a special tag inside for loops you can use for this
{% for category in entry.mediaType %}
    {{ category.title }},
    {% if loop.last %}
        {{ category.title }}
    {% endif %}
{% endfor %}

Full list here

> **Comments:**
> **username_0**: Awesome, I could not find this anywhere in the Craft docs, thank you!  
> **username_2**: You didn't find it in the Craft docs because [it's a native Twig variable](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable).  
> **username_6**: Is there an advantage to this vs wrapping the "," in a `{% if not loop.last %}`?  
> **username_1**: Personal preference.  

---

### Answer by username_1 on 2014-10-04. Score: 17

While your conditional is complete personally preference I would do the following as I find it slightly easier to read. Note the use of the {%- if -%} to trim whitespace:
{% for category in entry.mediaType %}
    {{ category.title }}
    {%- if not loop.last -%}
      ,
    {% endif %}
{% endfor %}

> **Comments:**
> **username_2**: To clean it up even further: `{{ not loop.last ? "," }}`  
> **username_3**: @LindseyD Your suggestion seems very neat but why do I get `unexpected char '?'` ?  
> **username_2**: @Pranasas I don’t know. I suggest starting a new thread, since this one is over 3 years old. Feel free to link back to this thread (or answer) for reference.  

---

### Answer by username_4 on 2018-12-11. Score: 5

{% for category in entry.mediaType %}
    {{ category.title }}{{ "," if not loop.last }}
{% endfor %}


---

