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
If users is ["Ana", "Ben", "Cara"], the output is:
Showing or hiding sections by count
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:
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:
or:
Dictionaries and strings
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:
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:
and render the result:
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.
Related reading
- Get list from pandas dataframe column or row?
- Get list of values for list of keys
- Get modulo from two 4x64bit integer arrays
- Get node list from random walk in networkX
- Get type of a generic parameter in Java with reflection
- GetProperties to return all properties for an interface inheritance hierarchy
- Get list of all routes defined in the Flask app
- Get list of pandas dataframe columns based on data type
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

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 courseData 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.