Jinja - Return the last element in a list
by
Jeremy Canfield |
Updated: November 28 2023
| Jinja articles
last is a Jinja filter that can be used to return the last element in a list. Take for example the following.
{% set fruits=['banana','apples','pineapples', 'grapes'] %}
<br /><br />
Original List:
{{ fruits }}
<br /><br />
Last Element in List:
{{ fruits | last }}
<br /><br />
Last Element in Sorted List:
{{ fruits | sort | last }}
Something like this should be returned.
Original List: ['banana', 'apples', 'pineapples', 'grapes']
Last Element in List: grapes
Last Element in Sorted List: pineapples
Did you find this article helpful?
If so, consider buying me a coffee over at