# merge 2 arrays into array of dictionaries

**URL:** <https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036>\
**Category:** Ansible Project\
**Created:** [December 16, 2015, 7:43pm UTC](https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036 "2015-12-16T19:43:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abel.makkonen.tesfay](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@abel.makkonen.tesfay](https://forum.ansible.com/u/abel.makkonen.tesfay)\
**Post date:** [December 16, 2015, 7:43pm UTC](https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036/1 "2015-12-16T19:43:11Z")

</div>

```auto
I have 2 arrays

```

```auto

```

```auto
"containers": [
            "ContainerA", 
            "ContainerA", 
            "ContainerB", 
            "ContainerB"
        ]

"apps": [
            "app2", 
            "app1", 
            "app2", 
            "app1"
        ]

I would like to merge them into an array of dictionaries like this ( I assume with set_fact ):

services:
- { container: 'ContainerA', app: 'app2' }
- { container: 'ContainerA', app: 'app1' }
- { container: 'ContainerB', app: 'app2' } 
- { container: 'ContainerB', app: 'app1' }

```

```auto

```

```auto
How can I do that?

```

---

<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:** [December 16, 2015, 8:03pm UTC](https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036/2 "2015-12-16T20:03:06Z")

</div>

How do you plan on using this data. Maybe there is a different way to achieve your result, because it won’t be easy or straight forward to get what you want.

Maybe with\_together on a task could give you what you want instead:

- debug: var=item  
with\_together:

- apps

- containers

---

<div class="post-metadata">

**Author:** ![abel.makkonen.tesfay](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@abel.makkonen.tesfay](https://forum.ansible.com/u/abel.makkonen.tesfay)\
**Post date:** [December 16, 2015, 8:19pm UTC](https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036/3 "2015-12-16T20:19:56Z")

</div>

```auto
I plan to use it in a template like so:

```

```auto
{% for source in services %}

```

```auto
container={{source.container}}

```

```auto
app={{source.app}}

```

```auto
{% endfor %}

```

---

<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:** [December 16, 2015, 9:58pm UTC](https://forum.ansible.com/t/merge-2-arrays-into-array-of-dictionaries/20036/4 "2015-12-16T21:58:45Z")

</div>

Perhaps the following will achieve your goal:

{% for source in lookup(‘together’, containers, apps, wantlist=True) %}  
container={{ source.0 }}  
app={{ source.1 }}  
{% endfor %}
