Conditionally join a list of strings in Jinja
jinja2, python
Solution
Use `reject` filter with `sameas` test:
>>> import jinja2
>>> template = jinja2.Template("{{ users|reject('sameas', 'tom')|join(',') }}")
>>> template.render(users=['tom', 'dick', 'harry'])
u'dick,harry'
UPDATE
If you're using Jinja 2.8+, use `equalto` instead of `sameas` as @Dougal commented; `sameas` tests with Python `is`, while `equalto` tests with `==`.
Problem
I have a list like ``` users = ['tom', 'dick', 'harry'] ``` In a `Jinja` template I'd like to print a list of all users except `tom` joined. I cannot modify the variable before it's passed to the template. I tried a list comprehension, and using Jinja's `reject` filter but I haven't been able to get these to work, e.g. ``` {{ [name for name in users if name != 'tom'] | join(', ') }} ``` gives a syntax error. How can I join list items conditionally?