Bootstrap FreeKB - Jinja - Remove a value from a list using reject
Jinja - Remove a value from a list using reject

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 2da832 in the box below so that we can be sure you are a human.