# ansible list in dict (need to understand how it works)

**URL:** <https://forum.ansible.com/t/ansible-list-in-dict-need-to-understand-how-it-works/36412>\
**Category:** Ansible Project\
**Created:** [March 8, 2022, 7:22pm UTC](https://forum.ansible.com/t/ansible-list-in-dict-need-to-understand-how-it-works/36412 "2022-03-08T19:22:04Z")\
**Posts on this page:** 3\
**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 8, 2022, 7:22pm UTC](https://forum.ansible.com/t/ansible-list-in-dict-need-to-understand-how-it-works/36412/1 "2022-03-08T19:22:04Z")

</div>

Hi everyone,

I try to understand ansible dictionaries and for my need I use lists in a dictionary which makes the code not simple and I am struggling to understand that:

Below there’re part of my playbook and result:

user\_list:

- name: “name1”  
city: “New York”  
phone: “03-45-67”
- name: “name2”  
city: “London”  
phone: “04-45-67”
- name: “name3”  
city: “Berlin”  
phone: “05-45-67”

I need to retrieve information like each phone number for each name and I found this solution:

- name: debugging dict  
debug:  
msg:
- “type of user\_list: {{ user\_list | type\_debug }}”
- " this {{ item[‘name’] }} is relation to this phone : {{ item[‘phone’] }}"  
loop: “{{ user\_list }}”

and here’s the result:

TASK [ansible\_dict\_test : debugging dict] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [localhost] =\> (item={‘name’: ‘name1’, ‘city’: ‘New York’, ‘phone’: ‘03-45-67’}) =\> {

“msg”: [

“type of user\_list: list”,

" this name1 is relation to this phone : 03-45-67"

]

}

ok: [localhost] =\> (item={‘name’: ‘name2’, ‘city’: ‘London’, ‘phone’: ‘04-45-67’}) =\> {

“msg”: [

“type of user\_list: list”,

" this name2 is relation to this phone : 04-45-67"

]

}

ok: [localhost] =\> (item={‘name’: ‘name3’, ‘city’: ‘Berlin’, ‘phone’: ‘05-45-67’}) =\> {

“msg”: [

“type of user\_list: list”,

" this name3 is relation to this phone : 05-45-67"

]

}

I’ve some question about that:

1. type\_debug read from user\_list and it is a dict then why it give a list as result?

2. I has understood, user\_list ist the key of dict and the rest is the value. In that case why item[‘name’] is like a key?

3. Is my playbook good or is a better solution?

Thank you in advance for your help because I am really trying to understand in detail how dictionaries work and I need your advice.

Best regards, H

---

<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:** [March 8, 2022, 11:27pm UTC](https://forum.ansible.com/t/ansible-list-in-dict-need-to-understand-how-it-works/36412/2 "2022-03-08T23:27:32Z")

</div>

> 1) type\_debug read from user\_list and it is a dict then why it give a list  
> as result?

The value of the variable \*user\_list\* is a list (you wouldn't be able  
to iterate it if otherwise)

&nbsp;&nbsp;user\_list:  
&nbsp;&nbsp;- city: New York  
&nbsp;&nbsp;&nbsp;&nbsp;name: name1  
&nbsp;&nbsp;&nbsp;&nbsp;phone: 03-45-67  
&nbsp;&nbsp;- city: London  
&nbsp;&nbsp;&nbsp;&nbsp;name: name2  
&nbsp;&nbsp;&nbsp;&nbsp;phone: 04-45-67  
&nbsp;&nbsp;- city: Berlin  
&nbsp;&nbsp;&nbsp;&nbsp;name: name3  
&nbsp;&nbsp;&nbsp;&nbsp;phone: 05-45-67

> 2) I has understood, user\_list ist the key of dict and the rest is the  
> value. In that case why item['name'] is like a key?

Yes. \*user\_list\* is dictionary (aka hash, or mapping) with one key  
\*user\_list\*. The value of this key is the list. Each item of this  
list is a dictionary therefore, in iteration, you can use \*name\* as a  
key in the dictionary \*item\*.

> 3) Is my playbook good or is a better solution?

The dictionaries can be searched faster compared to the lists

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_dict: "{{ dict(\_keys|zip(user\_list)) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_keys: "{{ user\_list|map(attribute='name')|list }}"

transforms the list to a dictionary

&nbsp;&nbsp;phone\_book:  
&nbsp;&nbsp;&nbsp;&nbsp;name1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;city: New York  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: name1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phone: 03-45-67  
&nbsp;&nbsp;&nbsp;&nbsp;name2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;city: London  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: name2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phone: 04-45-67  
&nbsp;&nbsp;&nbsp;&nbsp;name3:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;city: Berlin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: name3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phone: 05-45-67

Now you can easily search by names. If the names alone are not unique  
create the keys as a combination of attributes

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;phone\_book: "{{ dict(\_keys|zip(\_vals)) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_names: "{{ user\_list|map(attribute='name')|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_addrs: "{{ user\_list|map(attribute='city')|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_keys: "{{ \_names|zip(\_addrs)|map('join', ',')|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_vals: "{{ user\_list|map(attribute='phone')|list }}"

create keys by joining the name and the city, and values by selecting  
the phone only

&nbsp;&nbsp;phone\_book:  
&nbsp;&nbsp;&nbsp;&nbsp;name1,New York: 03-45-67  
&nbsp;&nbsp;&nbsp;&nbsp;name2,London: 04-45-67  
&nbsp;&nbsp;&nbsp;&nbsp;name3,Berlin: 05-45-67

You need the bracket notation to reference such keys

&nbsp;&nbsp;&nbsp;&nbsp;phone\_book['name2,London']: 04-45-67

You'll have to stick with the lists if you are not able to create  
unique keys.

---

<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 9, 2022, 3:16pm UTC](https://forum.ansible.com/t/ansible-list-in-dict-need-to-understand-how-it-works/36412/3 "2022-03-09T15:16:55Z")

</div>

Thanks a lot for your detailed instructions.  
I understand dictionnaires much better now but I still have to practice.  
I know we learn by doing but how do we know what tips or functions we need for a specific case. It is not easy to use something that we don’t know about, by example I didn’t know about zip.  
So I need to learn the other functions and how to involve them together.

Best regards, H
