# Removing empty strings from a list

**URL:** <https://forum.ansible.com/t/removing-empty-strings-from-a-list/29281>\
**Category:** Ansible Project\
**Created:** [December 18, 2018, 7:47am UTC](https://forum.ansible.com/t/removing-empty-strings-from-a-list/29281 "2018-12-18T07:47:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 18, 2018, 7:47am UTC](https://forum.ansible.com/t/removing-empty-strings-from-a-list/29281/1 "2018-12-18T07:47:07Z")

</div>

I am trying to build a list of names from a list of objects. It works fine, but some of the objects don’t have a name. No problem, I use the default() filter to set those elements of my list to empty strings, like this:

# Build a list of names from a list of things

- set\_fact:  
names: “{{ names |default() + [[item.name](http://item.name) |default(‘’) ] }}”  
with\_items: “{{ things }}”

This got me, as expected, this result (because at the moment, my test list of things has two items, and neither has a name attribute):

ok: [localhost] =\> {  
“names”: [  
“”,  
“”  
]  
}

Now, how can I remove those empty strings from my list? I have read numerous articles that mention “rejectattr”, but I don’t understand how to use it.

I tried this:

# Remove any empty elements from the list of instance profile names

- set\_fact:  
names: “{{ names |default() |reject(‘equalto’, ‘’) }}”

And got this mystifying output:

ok: [localhost] =\> {  
“names”: “\<generator object \_select\_or\_reject at 0x7f4ecd9c9aa0\>”  
}

I also tried using rejectattr on the first loop like this:

# Build a list of names from a list of things

- set\_fact:  
names: “{{ names |default() + [[item.name](http://item.name) |default(‘’) ] }}”  
with\_items: “{{ things |rejectattr(‘name’, ‘undefined’) }}”

That got me a list with ONE empty string:

ok: [localhost] =\> {  
“names”: [  
“”  
]  
}

Grateful for any pointers…

Regards, K.

PS: Pointers to USEFUL, COMPLETE examples of how to use rejectattr(), reject() and so on would be useful too. All the examples I have found seem to assume a huge amount of knowledge I don’t have, or are tiny fragments of code…

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [December 18, 2018, 9:27am UTC](https://forum.ansible.com/t/removing-empty-strings-from-a-list/29281/2 "2018-12-18T09:27:42Z")

</div>

> I am trying to build a list of names from a list of objects. It works fine, but some of the objects don’t have a name. No problem, I use the default() filter to set those elements of my list to empty strings, like this:
> 
> # Build a list of names from a list of things
> 
> - set\_fact:  
> names: “{{ names |default() + [[item.name](http://item.name) |default(‘’) ] }}”  
> with\_items: “{{ things }}”
> 
> This got me, as expected, this result (because at the moment, my test list of things has two items, and neither has a name attribute):
> 
> ok: [localhost] =\> {  
> “names”: [  
> “”,  
> “”  
> ]  
> }
> 
> Now, how can I remove those empty strings from my list? I have read numerous articles that mention “rejectattr”, but I don’t understand how to use it.

Well, the easiest way is not to put them in there in the first place.

`

- set\_fact:  
names: “{{ things | selectattr(‘name’, ‘defined’) | map(attribute=‘name’) | list }}”

`

> 

> I tried this:
> 
> # Remove any empty elements from the list of instance profile names
> 
> - set\_fact:  
> names: “{{ names |default() |reject(‘equalto’, ‘’) }}”
> 
> And got this mystifying output:
> 
> ok: [localhost] =\> {  
> “names”: “\<generator object \_select\_or\_reject at 0x7f4ecd9c9aa0\>”  
> }

map(), select(), and friends return generators; use the list filter (as I’ve done above) to convert it to a list.

> PS: Pointers to USEFUL, COMPLETE examples of how to use rejectattr(), reject() and so on would be useful too. All the examples I have found seem to assume a huge amount of knowledge I don’t have, or are tiny fragments of code…

They’re very simple filters, so useful, complete examples are going to _be_ tiny fragments of code. [http://jinja.pocoo.org/docs/latest/templates/#rejectattr](http://jinja.pocoo.org/docs/latest/templates/#rejectattr)

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 18, 2018, 10:45am UTC](https://forum.ansible.com/t/removing-empty-strings-from-a-list/29281/3 "2018-12-18T10:45:49Z")

</div>

Thanks, flowerysong.

I went with this:

- set\_fact:  
names: 
- set\_fact:  
names: “{{ names + [[item.name](http://item.name) ] }}”  
with\_items: “{{ things |selectattr(‘name’, ‘defined’) |list }}”

I want to have the list even if it is empty, that’s why there are now two set\_fact calls.

As far as the examples being complete, I have to disagree. These are ALL the examples for rejectattr() on the page you linked:

```auto
{{ users|rejectattr("is_active") }}
{{ users|rejectattr("email", "none") }}

```

Unanswered questions:

- what is “is\_active”?
- what is “email”?
- what is “none”?
- what is “users”?
- what are the results of using the two examples?

This filter (like most of the others) needs far more complete documentation. It should provide a sample “users” variable, and sample output for the two examples. It should describe clearly what each of the possible parameters to the filter is.

I have no idea whether ANY of the following is correct:

I’m assuming that users is a structured list and that “is\_active” and “email” are attributes of the elements of that list, e.g.:

vars:  
users:

- name: Fred Nurk"  
email: [fred@example.com](mailto:fred@example.com)  
is\_active: true

- name: “Bob McBobson”  
email: [bob@example.com](mailto:bob@example.com)  
is\_active: false

- name: “Mary Nurk”  
is\_active: true

I’m also guessing that “none” is some sort of filter. It’s described as a “test” - where are these tests documented? There is no description of it or link to other documentation, so I really have no idea. It may be selecting users where the email address is the literal string value “none”.

Give the above sample “users” variable, I am guessing that the expected output of the two examples, if assigned to x, would be:

x:

- name: “Fred Nurk”  
email: [fred@example.com](mailto:fred@example.com)  
is\_active: true
- name: “Mary Nurk”  
is\_active: true

x:

- name: Fred Nurk"  
email: [fred@example.com](mailto:fred@example.com)  
is\_active: true
- name: “Bob McBobson”  
email: [bob@example.com](mailto:bob@example.com)  
is\_active: false

Please don’t just tell me I am free to write the documentation myself 🙂 This is constructive criticism, not a complaint.

Regards, K.
