# How to remove a single item from a list within a nested dictionary

**URL:** <https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376>\
**Category:** Ansible Project\
**Created:** [August 25, 2023, 1:19pm UTC](https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376 "2023-08-25T13:19:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [August 25, 2023, 1:19pm UTC](https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376/1 "2023-08-25T13:19:39Z")

</div>

The source nested dictionary is for instance the following:  
vpc:  
provider:

- net\_type: ‘db’  
net\_ipv4\_subnet: ‘[10.0.0.0/16](http://10.0.0.0/16)’  
net\_ipv6\_subnet: ‘FD00::/64’  
net\_id: ‘0’  
nic\_ids:
- 1111111111
- 2222222222
- net\_type: ‘dns’  
net\_ipv4\_subnet: ‘[10.1.0.0/16](http://10.1.0.0/16)’  
net\_ipv6\_subnet: ‘FD01::/64’  
net\_id: ‘1’  
nic\_ids:
- 3333333333
- 4444444444
- net\_type: ‘ns’  
net\_ipv4\_subnet: ‘[10.2.0.0/16](http://10.2.0.0/16)’  
net\_ipv6\_subnet: ‘FD02::/64’  
net\_id: ‘2’  
nic\_ids:
- 5555555555
- 6666666666

We need to remove the item ''4444444444 from the list \<nic\_ids\> located inside the dictionary matched by the key/value net\_id==‘1’.

Selecting the \<nic\_ids\> list inside the right dictionary is easy with:  
vpc.provider | selectattr(‘net\_id’, ‘eq’, ‘1’) | map(attribute= ‘nic\_ids’), but I have no idea how to remove the item from list and build the new vpc.provider?

Any suggestion?

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [August 25, 2023, 2:22pm UTC](https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376/2 "2023-08-25T14:22:09Z")

</div>

Here is how to get the new list:

- name: Removing a \<nic\_id\> from the list inside the dictionary matching the \<net\_id\>  
vars:  
vpc:  
provider:
- net\_type: ‘db’  
net\_ipv4\_subnet: ‘10.0.0.0/16’  
net\_ipv6\_subnet: ‘FD00::/64’  
net\_id: ‘0’  
nic\_ids:
- 1111111111
- 2222222222
- net\_type: ‘dns’  
net\_ipv4\_subnet: ‘10.1.0.0/16’  
net\_ipv6\_subnet: ‘FD01::/64’  
net\_id: ‘1’  
nic\_ids:
- 3333333333
- 4444444444
- net\_type: ‘ns’  
net\_ipv4\_subnet: ‘10.2.0.0/16’  
net\_ipv6\_subnet: ‘FD02::/64’  
net\_id: ‘2’  
nic\_ids:
- 5555555555
- 6666666666  
nic\_ids\_:
- 4444444444  
new\_nic\_ids: “{{ vpc.provider | selectattr(‘net\_id’, ‘eq’, ‘1’) | map(attribute= ‘nic\_ids’) | first | difference(nic\_ids\_) }}”  
debug:  
msg: “{{ new\_nic\_ids }}”

But I don’t know how to get the updated .

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [August 25, 2023, 2:32pm UTC](https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376/3 "2023-08-25T14:32:09Z")

</div>

OK, I got it, but I’m sure there is a more elegant and simpler way to achieve the same result:

- name: Removing a \<nic\_id\> from the dictionary matching the \<net\_id\>  
vars:  
vpc:  
provider:
- net\_type: ‘db’  
net\_ipv4\_subnet: ‘10.0.0.0/16’  
net\_ipv6\_subnet: ‘FD00::/64’  
net\_id: ‘0’  
nic\_ids:
- 1111111111
- 2222222222
- net\_type: ‘dns’  
net\_ipv4\_subnet: ‘10.1.0.0/16’  
net\_ipv6\_subnet: ‘FD01::/64’  
net\_id: ‘1’  
nic\_ids:
- 3333333333
- 4444444444
- net\_type: ‘ns’  
net\_ipv4\_subnet: ‘10.2.0.0/16’  
net\_ipv6\_subnet: ‘FD02::/64’  
net\_id: ‘2’  
nic\_ids:
- 5555555555
- 6666666666  
nic\_ids\_:
- 4444444444  
new\_nic\_ids: “{{ vpc.provider | selectattr(‘net\_id’, ‘eq’, ‘1’) | map(attribute= ‘nic\_ids’) | first | difference(nic\_ids\_) }}”  
seed\_vpc:  
provider:
- net\_id: ‘1’  
nic\_ids: “{{ new\_nic\_ids }}”  
new\_vpc:  
provider:  
“{{ [vpc.provider, seed\_vpc.provider] | community.general.lists\_mergeby(‘net\_id’, recursive=True, list\_merge=‘replace’) }}”  
debug:  
msg: “{{ new\_vpc }}”

---

<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:** [August 25, 2023, 4:57pm UTC](https://forum.ansible.com/t/how-to-remove-a-single-item-from-a-list-within-a-nested-dictionary/38376/4 "2023-08-25T16:57:28Z")

</div>

Put the items you want to update into a dictionary. For example,

&nbsp;&nbsp;&nbsp;&nbsp;diff:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1': [4444444444]

Use Jinja to update the list

&nbsp;&nbsp;&nbsp;&nbsp;update: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% filter from\_yaml %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in vpc.provider %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if i.net\_id in diff %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% set nic\_ids=i.nic\_ids|difference(diff[i.net\_id]) %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {{ i|combine({'nic\_ids': nic\_ids}) }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {{ i }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfilter %}

Then, create the dictionary

&nbsp;&nbsp;&nbsp;&nbsp;new\_vpc:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;provider: "{{ update }}"
