# Remove items from a variable with reject

**URL:** <https://forum.ansible.com/t/remove-items-from-a-variable-with-reject/32956>\
**Category:** Ansible Project\
**Created:** [May 17, 2020, 8:57pm UTC](https://forum.ansible.com/t/remove-items-from-a-variable-with-reject/32956 "2020-05-17T20:57:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kernel\_Panic](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@Kernel\_Panic](https://forum.ansible.com/u/Kernel_Panic)\
**Post date:** [May 17, 2020, 8:57pm UTC](https://forum.ansible.com/t/remove-items-from-a-variable-with-reject/32956/1 "2020-05-17T20:57:57Z")

</div>

Hello  
I want to remove items from a variable using reject filter, I have something like this:

vars:  
myvar:

- item1
- item2

List is the output of a command which contains contains item1, item2 , item3, item4, item5, I want to remove item1 and 2 ( from myvar ) from the list with the code below  
but is not working, any idea what am I doing wrong of different way to accomplish this?

- set\_fact:  
list\_of\_items: “{{ list.stdout\_lines | reject(‘search’, item ) | list }}”  
with\_items: “{{ myvar }}”

Thanks  
Regards

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 17, 2020, 10:04pm UTC](https://forum.ansible.com/t/remove-items-from-a-variable-with-reject/32956/2 "2020-05-17T22:04:28Z")

</div>

Use filter "difference". For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list\_of\_items: "{{ list.stdout\_lines|difference(my\_var) }}"

HTH,  
&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Kernel\_Panic](https://avatars.discourse-cdn.com/v4/letter/k/db5fbb/32.png) [@Kernel\_Panic](https://forum.ansible.com/u/Kernel_Panic)\
**Post date:** [May 18, 2020, 12:07am UTC](https://forum.ansible.com/t/remove-items-from-a-variable-with-reject/32956/3 "2020-05-18T00:07:10Z")

</div>

That did the trick.!!!  
Thank you so much!!!
