# list of lists, to list of dicts?

**URL:** <https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860>\
**Category:** Ansible Project\
**Created:** [August 29, 2024, 9:40am UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860 "2024-08-29T09:40:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 29, 2024, 9:40am UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/1 "2024-08-29T09:40:07Z")

</div>

Hii

I have something like this in my vars:

test:

- 
  - host1

- fd99:9999::1
- 
  - host1

- 10.0.1.1
- 
  - host2

- fd99:9999::1
- 
  - host2

- 10.0.1.1
- 
  - foo

- fd99:9999::1
- 
  - foo

- 10.0.1.1
- 
  - baz

- fd99:9999::1
- 
  - baz

- 10.0.1.1

Each first and second list item should become keys in a dict, like this:

conv:

- name: host1  
address: fd99:9999::1
- name: host1  
address: 10.0.1.1
- name: host2  
address: fd99:9999::1
- name: host2  
address: 10.0.1.1
- name: foo  
address: fd99:9999::1
- name: foo  
address: 10.0.1.1
- name: baz  
address: fd99:9999::1
- name: baz  
address: 10.0.1.1

I tried a few things with mapping and filtering, but can’t see how to do it…  
any suggestions? Ideally no tasks, just filters…

thx

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 29, 2024, 11:22am UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/2 "2024-08-29T11:22:09Z")

</div>

- name: list of list to list of dict

hosts: localhost

become: false

gather\_facts: false

vars:

test:

- 
  - host1

- fd99:9999::1

- 
  - host1

- 10.0.1.1

- 
  - host2

- fd99:9999::1

- 
  - host2

- 10.0.1.1

- 
  - foo

- fd99:9999::1

- 
  - foo

- 10.0.1.1

- 
  - baz

- fd99:9999::1

- 
  - baz

- 10.0.1.1

tasks:

- name: test

debug: var=test

- name: list 2 dict

set\_fact:

list2dict: “{{ list2dict|default() + [thisdict] }}”

loop: “{{ test }}”

vars:

thisdict: { name: “{{ item.0 }}”, addr: “{{ item.1 }}” }

- name: list2dict

debug: var=list2dict

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 29, 2024, 11:38am UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/3 "2024-08-29T11:38:41Z")

</div>

[https://docs.ansible.com/ansible/latest/collections/community/general/dict\_filter.html](https://docs.ansible.com/ansible/latest/collections/community/general/dict_filter.html)

This is an even more elegant solution using the community.general.dict module:

- name: list2dict

set\_fact:

list2dict: \>-

{{ test | map(‘zip’, [‘name’, ‘addr’])

> map(‘map’, ‘reverse’)

> map(‘community.general.dict’) }}

- name: list2dict

debug: var=list2dict

Walter

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 29, 2024, 11:47am UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/4 "2024-08-29T11:47:23Z")

</div>

perfect, thx!!

---

<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:** [August 29, 2024, 12:34pm UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/5 "2024-08-29T12:34:09Z")

</div>

That is elegant. Much better than the abomination I came up with:

```auto
---
# dick-visser-03.yml
- name: Add helper keys to list of dicts
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    test:
      - - host1
        - fd99:9999::1
      - - host1
        - 10.0.1.1
      - - host2
        - fd99:9999::1
      - - host2
        - 10.0.1.1
      - - foo
        - fd99:9999::1
      - - foo
        - 10.0.1.1
      - - baz
        - fd99:9999::1
      - - baz
        - 10.0.1.1
  tasks:
    - name: Dictify the test data - 1
      ansible.builtin.debug:
        msg:
          - 'test'
          - '{{ test }}'

          - ' test | map("map","community.general.dict_kv","value")'
          - '{{ test | map("map","community.general.dict_kv","value") }}'

          - ' test | map("map","community.general.dict_kv","value") | map("zip",[{"key":"name"},{"key":"address"}])'
          - '{{ test | map("map","community.general.dict_kv","value") | map("zip",[{"key":"name"},{"key":"address"}]) }}'

          - ' test
                > map("map","community.general.dict_kv","value")
                > map("zip",[{"key":"name"},{"key":"address"}])
                > map("map","flatten",1)
                > map("map","combine")'
          - '{{ test
                > map("map","community.general.dict_kv","value")
                > map("zip",[{"key":"name"},{"key":"address"}])
                > map("map","flatten",1)
                > map("map","combine") }}'

          - ' test
                > map("map","community.general.dict_kv","value")
                > map("zip",[{"key":"name"},{"key":"address"}])
                > map("map","flatten",1)
                > map("map","combine")
                > map("items2dict")'
          - '{{ test
                > map("map","community.general.dict_kv","value")
                > map("zip",[{"key":"name"},{"key":"address"}])
                > map("map","flatten",1)
                > map("map","combine")
                > map("items2dict") }}'

```

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 29, 2024, 1:03pm UTC](https://forum.ansible.com/t/list-of-lists-to-list-of-dicts/38860/6 "2024-08-29T13:03:02Z")

</div>

Thanks Todd. Honestly I came right off the dict module page that has an almost exact example.

Walter
