Get count of list items that meet a condition with Jinja2
<p>I have a list of dictionaries where each dict has a boolean entry. I want to display the items that are <code>True</code>, along with the count of those items. I'm using the <code>selectattr</code> filter, but it returns a generator, and calling <code>|length</code> on it raise an error.  How can I get the length of the items returned from <code>selectattr</code> in Jinja?</p>

<pre><code>my_list = [{foo=False, ...}, {foo=True, ...}, ...]
</code></pre>



<pre><code>{{ my_list|selectattr('foo', 'equalto', True)|length }}
</code></pre>

<p>There is a <code>list</code> filter that will transform a generator into a list.  So:</p>

<pre><code>{{ my_list|selectattr('foo')|list|length }}
</code></pre>

python flask jinja2