user3501855 user3501855 - 1 year ago 205
Python Question

Get count of list items that meet a condition with Jinja2

I have a list of dictionaries where each dict has a boolean entry. I want to display the items that are

, along with the count of those items. I'm using the
filter, but it returns a generator, and calling
on it raise an error. How can I get the length of the items returned from
in Jinja?

my_list = [{foo=False, ...}, {foo=True, ...}, ...]

{{ my_list|selectattr('foo', 'equalto', True)|length }}

Answer Source

There is a list filter that will transform a generator into a list. So:

{{ my_list|selectattr('foo')|list|length }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download