# How to convert a dictionary to a list with key=value elements or to a string with key=value elements?

**URL:** <https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830>\
**Category:** Ansible Project\
**Created:** [October 19, 2018, 5:55pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830 "2018-10-19T17:55:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 19, 2018, 5:55pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/1 "2018-10-19T17:55:24Z")

</div>

Given an arbitrary dictionary like:

`test: one: wan two: too three: tree`

How can I convert it to a list like:

`  
test:

- one=wan
- two=too
- thee=tree  
`

The end result that I need is actually a string of these list items (“one=one, two=too, three=tree”). I know that I can easily `join()` a list of items, but if there is an easier way to directly convert the dictionary to a string, that would work too.

Again, I know neither the number of dictionary elements, nor the key names.

A solution that works in version 2.5 would be best…

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 19, 2018, 6:03pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/2 "2018-10-19T18:03:40Z")

</div>

This should do the trick:

{{ test|dictsort|map(‘join’, ‘=’)|join(', ') }}

In my test, it produces:

ok: [localhost] =\> {  
“msg”: “one=wan, three=tree, two=too”  
}

It’s ordered slightly different than your version, and dictsort could help change the sort order, however just a note that python dictionaries in most python versions are unsorted.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 19, 2018, 8:06pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/3 "2018-10-19T20:06:01Z")

</div>

I’ll give that a try, thank you.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 20, 2018, 3:59pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/4 "2018-10-20T15:59:07Z")

</div>

Is there a way to modify this so that the item to the right of the equal sign was quoted?

So… one=“wan”, two=“too”, three=“tree”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 20, 2018, 5:01pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/5 "2018-10-20T17:01:19Z")

</div>

You can always use Jinja template

&nbsp;&nbsp;- debug: msg="{% for k, v in test.iteritems() %}{{ k }}="{{ v }}"{% if not loop.last %}, {% endif %}{% endfor %}"

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 20, 2018, 9:00pm UTC](https://forum.ansible.com/t/how-to-convert-a-dictionary-to-a-list-with-key-value-elements-or-to-a-string-with-key-value-elements/28830/6 "2018-10-20T21:00:27Z")

</div>

That is what I ended up doing, but was curious if there is a ‘filter’ way.
