# How to convert a dictionary to a list of dictionaries

**URL:** <https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733>\
**Category:** Ansible Project\
**Created:** [June 14, 2022, 11:25am UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733 "2022-06-14T11:25:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [June 14, 2022, 11:25am UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/1 "2022-06-14T11:25:06Z")

</div>

I’m faced with a corner case where some devices are expected to return a list of dictionaries but instead return a single dictionary. This creates an error later on in ansible when a list is expected.  
For instance, the device returns the file:  
var:

key\_1: value\_11  
key\_2: value\_21  
key\_3: value\_31

instead of for instance:

var:

- key\_1: value\_11  
key\_2: value\_21  
key\_3: value\_31
- key\_1: value\_12  
key\_2: value\_22  
key\_3: value\_32  
…

So the goal is to transform the first var into:  
var:

- key\_1: value\_11  
key\_2: value\_21  
key\_3: value\_31

AFAIK, there is no [ansible filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#transforming-dictionaries-into-lists) to accomplish that; for instance, ‘list’ does not give the expected result.  
Also, we cannot count on the name or number of keys as they may change.  
I suppose the solution may revolve around something like that:

- set\_fact:  
list\_var: \>-  
{% for key, value in var.items() %}  
{{ [{key: value}] }}  
{% endfor %}  
when: \>  
(var | type\_debug != ‘list’)

This does not give the expected result either.

Any suggestion?

---

<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:** [June 14, 2022, 1:35pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/2 "2022-06-14T13:35:28Z")

</div>

Simply close it in the brackets. For example,

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ [var] }}"

gives

&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;- key\_1: value\_11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_2: value\_21  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_3: value\_31

, or

&nbsp;&nbsp;var2: "{{ [var] + [var] }}"

gives

&nbsp;&nbsp;var2:  
&nbsp;&nbsp;&nbsp;&nbsp;- key\_1: value\_11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_2: value\_21  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_3: value\_31  
&nbsp;&nbsp;&nbsp;&nbsp;- key\_1: value\_11  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_2: value\_21  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key\_3: value\_31

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [June 14, 2022, 1:49pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/3 "2022-06-14T13:49:20Z")

</div>

So simple!  
Nice! 🙂

---

<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:** [June 14, 2022, 2:18pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/4 "2022-06-14T14:18:40Z")

</div>

This works, but if the original var is already a list, you get a list  
of a list, which is probably not what the OP wants.  
In cases like this I tend to use the "to\_array" function of jmespath:

[https://jmespath.org/specification.html#to-array](https://jmespath.org/specification.html#to-array)

This will always return a list. Example:

---

<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:** [June 14, 2022, 2:39pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/5 "2022-06-14T14:39:41Z")

</div>

many ways to do it conditionally without need for  
serializing/deserializing into jaon

var is dict|ternary([var], var)

---

<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:** [June 14, 2022, 7:39pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/6 "2022-06-14T19:39:56Z")

</div>

> many ways to do it conditionally without need for  
> serializing/deserializing into jaon
> 
> var is dict|ternary([var], var)

With this I get:

&nbsp;&nbsp;&nbsp;No test named ''dict''

Shouldn't it be 'mapping' ?

---

<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:** [June 15, 2022, 2:31pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-of-dictionaries/36733/7 "2022-06-15T14:31:38Z")

</div>

yes, 'mapping' is correct ... this what happens when you respond away  
from the computer and don't verify ....
