Menu
Jinja First: filter function to retrieve first item – Jinja Documentation

Jinja First: filter function to retrieve first item

The Handbook to the JinjafirstFilter

The Jinja

filter is a straightforward and highly useful tool for retrieving the very first item from a sequence. This filter is the opposite of the

filter and is essential for tasks where you need to access a single, initial element without having to iterate over the entire collection. Whether you're displaying the first name from a list of users, the initial character of a string, or the first item that matches a certain condition, the

filter simplifies your template logic and makes your code more readable.

How thefirstFilter Works

filter takes an iterable (like a list, tuple, or string) and returns the first item it encounters. The process is very efficient because the filter stops as soon as it finds the first element; it doesn't need to process the rest of the sequence. The basic syntax is as follows:

text
{{ my_sequence | first }}

A key feature of the

filter is how it handles different types of iterables. If the sequence is empty, the filter returns an

object, which is Jinja's way of indicating that no value was found. This prevents runtime errors and allows you to build conditional logic around the filter's output.

Practical Examples

Let's explore some practical examples to see how the

filter can be used to streamline your templates.

This is the most common use case. You have a list of items and you only need to display the first one.

text
{% set users = ['Alice', 'Bob', 'Charlie'] %}
<p data-translate="true">Welcome, {{ users | first }}!</p>
text
<p data-translate="true">Welcome, Alice!</p>

filter quickly grabs

from the list, making the code concise and efficient.

Strings are also sequences of characters, so the

filter works on them just as well.

text
{% set my_name = "Jinja" %}
<p data-translate="true">My initial is {{ my_name | first }}.</p>
text
<p data-translate="true">My initial is J.</p>

This can be useful for displaying initials or other similar formatting needs.

The real power of the

filter comes when you combine it with other filters, like

. This allows you to find the first item in a sequence that meets a specific condition.

text
{% set products = [
    {'name': 'Jinja T-Shirt', 'price': 25},
    {'name': 'Flask Mug', 'price': 15},
    {'name': 'Python Book', 'price': 40}
] %}

{% set first_expensive_product = products | selectattr('price', '>', 20) | first %}
{% if first_expensive_product %}
<p data-translate="true">The first expensive product is: {{ first_expensive_product.name }}</p>
{% endif %}
text
<p data-translate="true">The first expensive product is: Jinja T-Shirt</p>

In this example,

creates an iterator of products with a price greater than 20, and the

filter immediately takes the first one from that iterator. The conditional

check ensures that the paragraph is only rendered if an expensive product is found.

As mentioned earlier, the

filter returns

on an empty sequence. This allows for clean conditional rendering.

text
{% set favorite_colors = [] %}
<p data-translate="true">My first favorite color is: {{ (favorite_colors | first) | default('not specified') }}</p>
text
<p data-translate="true">My first favorite color is: not specified</p>

Here, since

is an empty list,

returns

filter then gracefully provides a fallback message.

Conclusion

The Jinja

filter is an essential and highly efficient tool for any developer working with sequences. Its ability to quickly retrieve the initial item, combined with its graceful handling of empty sequences, allows you to write more readable and robust templates. By using

in combination with other filters, you can perform powerful data lookups without the need for complex template logic. It is a fundamental filter that simplifies data presentation and makes your code cleaner and more expressive.