# create a dict from a list (value of another dict)

**URL:** <https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [March 15, 2022, 3:51pm UTC](https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443 "2022-03-15T15:51:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 15, 2022, 3:51pm UTC](https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443/1 "2022-03-15T15:51:19Z")

</div>

Hi Team,

My playbook role receive a dict coming from a lookup file, **linux\_file\_list** is the variable name of this lookup. This dict has only one key and some lists as value. Elements of these lists are dict too.

Here’s my debug to see the content:

- Name: debugging  
debug:  
msg:“{{ linux\_file\_list | dict2items }}”

And here’s the result (a part…):

TASK [reposync : debugging] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: =\> {

“msg”: [

[

{

“key”: “list\_of\_linux”,

“value”: [

{

“description”: “linux SLES”,

“enabled”: 1,

“version”: 15,

“name”: “sles”

},

{

“description”: “linux RHEL”,

“enabled”: 1,

“version”: 8.4,

“name”: “rhel”

}

]

}

]

]

}

Now, I want to define the name value as a dict to loop on this to have at the end a loop to retrieve these variables, like so…

“name: {{ item[‘name’] }}”  
“description: {{ item[‘description’] }}”  
“version: {{ item[‘version’] }}”  
etc…

Unfortunately I don’t find how can i do that. I’ve try many things, like item.value[0][0] to have the first element of the first list but it doesn’t run.

With set\_fact i cannot do a dict on the list too:

- set\_fact:  
dict: “{{ dict(\_keys|zip(linux\_file\_list)) }}"  
vars:  
\_keys: “{{ item[‘value’]|map(attribute=‘name’)|list }}”  
loop: “{{ linux\_file\_list | dict2items }}”

As anyone an Idea how can I resolve this? I can’t see my problem clearly anymore and I need your help

Thanks in advance for your help, H

---

<div class="post-metadata">

**Author:** ![Hamza\_Bouabdallah](https://avatars.discourse-cdn.com/v4/letter/h/439d5e/32.png) [@Hamza\_Bouabdallah](https://forum.ansible.com/u/Hamza_Bouabdallah)\
**Post date:** [March 16, 2022, 9:41am UTC](https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443/2 "2022-03-16T09:41:58Z")

</div>

try the **subelement** filter

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [March 16, 2022, 4:46pm UTC](https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443/3 "2022-03-16T16:46:24Z")

</div>

I’ve try some subelements but cannot retrieve the right data.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 16, 2022, 8:45pm UTC](https://forum.ansible.com/t/create-a-dict-from-a-list-value-of-another-dict/36443/4 "2022-03-16T20:45:17Z")

</div>

> I've try some subelements but cannot retrieve the right data.  
> ie

I don't get why you use the dict2items filter on a list of dicts. You can start producing output from the "linux\_file\_list" variable directly.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
