# Creating a list of dicts from a list - solution

**URL:** <https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385>\
**Category:** Get Help\
**Created:** [December 30, 2024, 1:16pm UTC](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385 "2024-12-30T13:16:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sparky](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sparky/32/7086_2.png) [@sparky](https://forum.ansible.com/u/sparky)\
**Post date:** [December 30, 2024, 1:16pm UTC](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385/1 "2024-12-30T13:16:23Z")

</div>

Continuing the discussion from [creating a list of dicts from a list](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list/27332):

I had this same requirement. This article keeps coming up in the google search results, but without a solution. Unfortunately the article is in the Archives and I can’t respond to it. I’m adding the solution I finally discovered for ‘future me’ or someone else that may stumble upon this.

Solution to @Tim1’s and my problem:

```
- name: "Create list of dict"
  vars:
    list1:
      - "foo"
      - "bar"
      - "baz"
  ansible.builtin.set_fact:
    list2: "{{ list1 | map('community.general.dict_kv', 'name') }}"

```

---

<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:** [December 30, 2024, 4:20pm UTC](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385/2 "2024-12-30T16:20:26Z")

</div>

For further reference, here’s some more documentation on the `community.general.dict_kv` filter: [Dictionaries — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide_abstract_informations_dictionaries.html)

Its parent document, [community.general Filter Guide — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide.html), has some more info on other filters in community.general, and then there’s also [Using filters to manipulate data — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html). If someone happens to have a related problem for which the above solution doesn’t work exactly, these links might be helpful.

---

<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:** [December 30, 2024, 10:18pm UTC](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385/3 "2024-12-30T22:18:56Z")

</div>

FWIW, _json\_query_ gives the same result

```yaml
_query: '[].{"name": @}'
list2: "{{ list1 | json_query(_query) }}"

```

---

<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:** [April 14, 2025, 2:21am UTC](https://forum.ansible.com/t/creating-a-list-of-dicts-from-a-list-solution/39385/4 "2025-04-14T02:21:36Z")

</div>

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