Jinja - Remove a value from a list using reject
by
Jeremy Canfield |
Updated: November 28 2023
| Jinja articles
reject is a Jinja filter that can be used to remove one or more elements from a list. For example, here is how you could remove elements matching string "grape" from the fruits list.
{% set fruits=['apple','banana','grape','orange'] %}
Original List:
{% for fruit in fruits %}
{{ fruit }}
{% endfor %}
<br /><br />
Modified List:
{% for fruit in fruits | reject('eq', 'grape') %}
{{ fruit }}
{% endfor %}
Something like this should be returned.
Original List: apple banana grape orange
Modified List: apple banana orange
Did you find this article helpful?
If so, consider buying me a coffee over at