# How to extract a list of values of a field within a list of dictionaries when a key matches another list of values

**URL:** <https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296>\
**Category:** Ansible Project\
**Created:** [February 5, 2022, 2:56pm UTC](https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296 "2022-02-05T14:56:36Z")\
**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:** [February 5, 2022, 2:56pm UTC](https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296/1 "2022-02-05T14:56:36Z")

</div>

Let’s assume the following fictional list of dictionaries:

domain\_definition:

- name: server11  
cluster: cluster1  
db\_servers:
- server12
- server21  
port: ‘8080’
- name: server12  
cluster: cluster1  
db\_servers:
- server22  
port: ‘8090’
- name: server13  
cluster: cluster1  
port: ‘8091’
- name: server21  
cluster: cluster2  
db\_servers:
- server12
- server22  
port: ‘9080’
- name: server22  
cluster: cluster2  
port: ‘9090’

The goal is to list all ports of used ‘db\_servers’, so the expected list is:

- ‘8090’
- ‘9080’
- ‘9090’
- ‘8090’
- ‘9090’

The **constraint** is to use one (some operation is performed for each item) and only one loop (the list is huge in reality).

The following does not work because selectattr expects a value, not a list of values:

- name: Extracting a list of values of a field within a list of dictionaries when a key matches another list of values  
hosts:
- localhost  
strategy: debug  
tasks:
- name: Extracting a list of values of a field within a list of dictionaries when a key matches another list of values  
vars:  
domain\_definition:
- name: server11  
cluster: cluster1  
db\_servers:
- server12
- server21  
port: ‘8080’
- name: server12  
cluster: cluster1  
db\_servers:
- server22  
port: ‘8090’
- name: server13  
cluster: cluster1  
port: ‘8091’
- name: server21  
cluster: cluster2  
db\_servers:
- server12
- server22  
port: ‘9080’
- name: server22  
cluster: cluster2  
port: ‘9090’  
db\_servers\_names: “{{ item.db\_servers |  
default() |  
list }}”  
db\_servers\_used\_ports: “{{ domain\_definition |  
selectattr(‘name’, ‘eq’, db\_servers\_names) |  
map(attribute= ‘port’) |  
list }}”  
debug:  
msg:
- “db\_servers\_names: {{ db\_servers\_names }}”
- “db\_servers\_used\_ports: {{ db\_servers\_used\_ports }}”  
loop: “{{ domain\_definition }}”

Any suggestion?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 6, 2022, 11:25pm UTC](https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296/2 "2022-02-06T23:25:26Z")

</div>

Instead of  
selectattr(‘name’, ‘eq’, db\_servers\_names)  
try this:  
selectattr(‘name’, ‘in’, db\_servers\_names)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 7, 2022, 1:32am UTC](https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296/3 "2022-02-07T01:32:18Z")

</div>

This produces the list you want:

```auto
---
- name: Extracting a list of values of a field within a list of dictionaries when a key matches another list of values
  hosts:
    - localhost
  strategy: debug
  tasks:
    - name: Extract a list of values of a field within a list of dictionaries when a key matches another list of values
      set_fact:
        db_used_ports: |
            {% set ports=[] %}
            {% for dd0 in domain_definition %}
            {% set db_servers_names=dd0.db_servers|default([]) %}
            {% for dd1 in domain_definition %}
            {% if dd1['name'] in db_servers_names %}
            {% set _ = ports.append(dd1.port) %}
            {% endif %}
            {% endfor %}
            {% endfor %}{{ ports }}"
      vars:
        domain_definition:
          - name: server11
            cluster: cluster1
            db_servers:
              - server12
              - server21
            port: '8080'
          - name: server12
            cluster: cluster1
            db_servers:
              - server22
            port: '8090'
          - name: server13
            cluster: cluster1
            port: '8091'
          - name: server21
            cluster: cluster2
            db_servers:
              - server12
              - server22
            port: '9080'
          - name: server22
            cluster: cluster2
            port: '9090'

    - name: Show the extracted ports
      debug:
        msg: "{{ db_used_ports }}"

```

---

<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:** [February 7, 2022, 8:52am UTC](https://forum.ansible.com/t/how-to-extract-a-list-of-values-of-a-field-within-a-list-of-dictionaries-when-a-key-matches-another-list-of-values/36296/4 "2022-02-07T08:52:47Z")

</div>

Thanks, your first answer is exactly what I was looking for. 🙂
