# Trying to use selectattr to filter where attribute is not an empty list

**URL:** <https://forum.ansible.com/t/trying-to-use-selectattr-to-filter-where-attribute-is-not-an-empty-list/21917>\
**Category:** Ansible Project\
**Created:** [July 4, 2016, 4:40pm UTC](https://forum.ansible.com/t/trying-to-use-selectattr-to-filter-where-attribute-is-not-an-empty-list/21917 "2016-07-04T16:40:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Gibson](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_Gibson](https://forum.ansible.com/u/Mark_Gibson)\
**Post date:** [July 4, 2016, 4:40pm UTC](https://forum.ansible.com/t/trying-to-use-selectattr-to-filter-where-attribute-is-not-an-empty-list/21917/1 "2016-07-04T16:40:01Z")

</div>

Hi

I’m trying to select a set of objects where an attribute (of type list) is not empty.

`  
mylist:

- name: name1  
list: 
- name: name2  
list:
- item1
- item2  
`

There seems to be no jinja2 test (in the documentation at least) that allows one to test on empty / notempty.

I want to be able to do something like:

`mylist>selectattr( 'list', 'notempty' )`

Is there a nice Ansible/Jinja2 trick that I’m missing that can do what I want?

Thanks  
Mark

---

<div class="post-metadata">

**Author:** ![mconlon](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mconlon](https://forum.ansible.com/u/mconlon)\
**Post date:** [June 21, 2017, 7:05pm UTC](https://forum.ansible.com/t/trying-to-use-selectattr-to-filter-where-attribute-is-not-an-empty-list/21917/2 "2017-06-21T19:05:25Z")

</div>

I just had this exact problem. Hopefully this is helpful to anyone else who comes across this thread.

An empty list is falsey, so

mylist\>selectattr(‘list’)

should get what you’re looking for, and

mylist\>rejectattr(‘list’)

would return elements that have an empty list.
