# Get key from a list of dict

**URL:** <https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799>\
**Category:** Get Help\
**Tags:** jinja2\
**Created:** [June 26, 2024, 8:54pm UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799 "2024-06-26T20:54:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [June 26, 2024, 8:54pm UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/1 "2024-06-26T20:54:19Z")

</div>

Hello,

from the xml module I get a list of dict of interfaces:

```auto
TASK [Show result] *****************************************************************************************************
ok: [opnsense] => {
    "iface_current.matches": [
        {
            "lo0": null
        },
        {
            "lan": null
        },
        {
            "wan": null
        },
        {
            "opt1": null
        },
        {
            "opt2": null
        },
        {
            "opt3": null
        }
    ]
}

```

Any idea how to extract the keys as a list? Best would be without a loop, only with map etc.

The expected result should be:

```auto
"iface_current.matches": ["lo0", "lan", "wan", "opt1", "opt2", "opt3"]

```

Thanks a lot! 🙂

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [June 26, 2024, 11:48pm UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/2 "2024-06-26T23:48:21Z")

</div>

This should be possible using `dict2items` filter like so:

```auto
iface_current_matches: "{{ iface_current.matches | dict2items | map(attribute='key') }}"

```

---

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [June 27, 2024, 5:36am UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/3 "2024-06-27T05:36:34Z")

</div>

… does not work, because iface\_current.matches is a list and dict2items expects a dictionary as input…

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [June 27, 2024, 7:05am UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/4 "2024-06-27T07:05:49Z")

</div>

You can use the `map` filter ([Template Designer Documentation — Jinja Documentation (3.0.x)](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.map)) to apply `dict2items` to every element of the list. Then you can use `map` with the `first` filter to map the list of lists to a list, and `map` with `attribute='key'` to get hold of the keys. Something like this:

```auto
iface_current_matches: "{{ iface_current.matches | map('dict2items') | map('first') | map(attribute='key') }}"

```

---

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [June 27, 2024, 8:11am UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/5 "2024-06-27T08:11:55Z")

</div>

ah, works perfect! Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 27, 2024, 8:12am UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799/6 "2024-07-27T08:12:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
