okoboko
okoboko

Reputation: 4482

Comma Separating List Values in a Jinja template

I'm using a for loop to create a list like [1, 2, 3]. Here's my Jinja template, which produce some Javascript:

xAxis: {
                categories: {
                    [
                    {% for data in records['result'] %}
                        {{ data['_id']['day'] }},
                    {% endfor %}
                ]
            },

It runs fine and generates the expected result, but my IDE (PyCharm) complains that the final comma is unnecessary (it isn't): [1,2,3,] instead of [1,2,3].

Is there a better way to place a comma at end end (e.g. convert to string first and concatenate the comma to the end)? Or, should I ignore the warning?

Upvotes: 0

Views: 4236

Answers (1)

Maxime Lorant
Maxime Lorant

Reputation: 36161

If you are using Jinja (it seems to be the case), you can use the join filter directly:

xAxis: {
            categories: {
                [{{ records['result']|join(', ', attribute='_id.day') }}]
        },

The attribute syntax allows to get subkey by separating them with a dot, according to the source code.

Upvotes: 2

Related Questions