jinja2
list length
template inheritance
python templating
web development

Jinja2 length of a list: the length filter

Codemia Knowledge Hub ·

Use the length filter: {{ users|length }}. It works on lists, tuples, strings, dictionaries and many other collections, and it is usually all you need for counts and conditional rendering in a template.

The length filter

jinja2
{{ users|length }}

If users is ["Ana", "Ben", "Cara"], the output is:

text
3

Showing or hiding sections by count

jinja2
1{% if users|length > 0 %}
2  <p>There are {{ users|length }} users.</p>
3{% else %}
4  <p>No users found.</p>
5{% endif %}

Hiding or showing a section based on how many items exist is one of the most common uses. It keeps simple presentation decisions in the template without extra boolean flags from the view code.

for ... else for empty states

Jinja2's for loop supports an else branch that runs when the collection is empty:

jinja2
1<ul>
2{% for user in users %}
3  <li>{{ user }}</li>
4{% else %}
5  <li>No users available.</li>
6{% endfor %}
7</ul>

For an empty-state message alone, for ... else is often cleaner than checking the length. Use length when the number itself appears in the output.

Nested collections

The filter works on nested attributes too, which is common in dashboards, tables and summary cards:

jinja2
<p>{{ post.comments|length }} comments</p>

or:

jinja2
{% if category.products|length >= 10 %}
  <p>Large category</p>
{% endif %}

Dictionaries and strings

jinja2
{{ settings|length }}
{{ title|length }}

For a dictionary, length returns the number of keys. For a string, it returns the character count. Because the meaning changes with the type, know what kind of object you are passing into the template. If the value can be None, decide how to handle that before rendering, because calling length on None is not the same as on an empty list.

Truthiness when the count does not matter

An empty list is falsey in Jinja2, so you can test it directly:

jinja2
{% if users %}
  <p>Users exist.</p>
{% endif %}

That reads better than users|length > 0 when only presence matters.

Counting in Python instead

Counting a plain in-memory list in the template is fine. Expensive or business-critical logic, such as a count that needs a database query, belongs in Python. Compute it in the view:

python
return render_template("users.html", users=users, user_count=len(users))

and render the result:

jinja2
<p>{{ user_count }} users</p>

The template stays simple and the application logic stays where it belongs. Having length available is not a reason to push application logic into the template: keep business decisions and expensive queries in Python code, and let templates express intent directly.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms