# merging lists

**URL:** <https://forum.ansible.com/t/merging-lists/37896>\
**Category:** Ansible Project\
**Created:** [April 17, 2023, 10:22pm UTC](https://forum.ansible.com/t/merging-lists/37896 "2023-04-17T22:22:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [April 17, 2023, 10:22pm UTC](https://forum.ansible.com/t/merging-lists/37896/1 "2023-04-17T22:22:10Z")

</div>

i want to do this, but i'm not getting exactly what i want

lista:  
- { one: 'str1', two: 'str2' }

listb:  
- "{{ lookup('vars','lista') }}"  
- { one: 'str1', two: 'str2' }

the output i want is

listb:  
{ one: 'str1', two: 'str2' },  
{ one: 'str1', two: 'str2' }

but what i get is

listb:  
[{ one: 'str1', two: 'str2' }],  
{ one: 'str1', two: 'str2' }

is there a way to merge the two lists?

---

<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:** [April 17, 2023, 11:05pm UTC](https://forum.ansible.com/t/merging-lists/37896/2 "2023-04-17T23:05:28Z")

</div>

The lookup \*vars\* is not necessary. Reference the variable

&nbsp;&nbsp;listb:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ lista }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- {one: str1, two: str2}

You should get

&nbsp;&nbsp;listb:  
&nbsp;&nbsp;&nbsp;&nbsp;- - one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2  
&nbsp;&nbsp;&nbsp;&nbsp;- one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2

This is correct. The first item on the list \*listb\* is list \*lista\*.  
If you put two items into the list \*lista\*

&nbsp;&nbsp;lista:  
&nbsp;&nbsp;&nbsp;&nbsp;- one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2  
&nbsp;&nbsp;&nbsp;&nbsp;- ccc: str3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddd: str4

you'll get

&nbsp;&nbsp;listb:  
&nbsp;&nbsp;&nbsp;&nbsp;- - one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ccc: str3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddd: str4  
&nbsp;&nbsp;&nbsp;&nbsp;- one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2

You can \*flatten\* the list

&nbsp;&nbsp;&nbsp;listc: "{{ listb|flatten }}"

gives

&nbsp;&nbsp;listc:  
&nbsp;&nbsp;&nbsp;&nbsp;- one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2  
&nbsp;&nbsp;&nbsp;&nbsp;- ccc: str3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ddd: str4  
&nbsp;&nbsp;&nbsp;&nbsp;- one: str1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two: str2

However, a simpler option is adding lists. For example, given two  
lists

&nbsp;&nbsp;lista:  
&nbsp;&nbsp;&nbsp;&nbsp;- {one: str1, two: str2}  
&nbsp;&nbsp;&nbsp;&nbsp;- {ccc: str3, ddd: str4}  
&nbsp;&nbsp;listb:  
&nbsp;&nbsp;&nbsp;&nbsp;- {one: str1, two: str2}

Declare the list \*listc\*. This will merge the two lists

&nbsp;&nbsp;listc: "{{ lista + listb }}"

---

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [April 17, 2023, 11:29pm UTC](https://forum.ansible.com/t/merging-lists/37896/3 "2023-04-17T23:29:02Z")

</div>

thanks for the help, but that's not exactly what i am after. i'm  
attempting to avoid creating a third variable. but maybe that's the  
only way

---

<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:** [April 18, 2023, 5:00am UTC](https://forum.ansible.com/t/merging-lists/37896/4 "2023-04-18T05:00:22Z")

</div>

> thanks for the help, but that's not exactly what i am after. i'm  
> attempting to avoid creating a third variable. but maybe that's the  
> only way

You can avoid the third variable if you want to. For example,

&nbsp;&nbsp;b: "{{ a + [4, 5, 6] }}"

will add the lists

&nbsp;&nbsp;b: [1, 2, 3, 4, 5, 6]

---

<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:** [April 18, 2023, 8:37am UTC](https://forum.ansible.com/t/merging-lists/37896/5 "2023-04-18T08:37:34Z")

</div>

If lista will only ever contain a single item, then listb could be just

listb:

- “{{ lista[0] }}”
- { one: ‘str1’, two: ‘str2’ }

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 12:56pm UTC](https://forum.ansible.com/t/merging-lists/37896/6 "2023-04-18T12:56:51Z")

</div>



---

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [April 18, 2023, 1:07pm UTC](https://forum.ansible.com/t/merging-lists/37896/7 "2023-04-18T13:07:30Z")

</div>

i got to this point as well in my testing, but the double list  
expansion of listb is a problem.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 1:08pm UTC](https://forum.ansible.com/t/merging-lists/37896/8 "2023-04-18T13:08:52Z")

</div>

This isn’t a double expansion of listb. lista and listb happen to have the same values. change one of them and run the playbook to prove it.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 1:10pm UTC](https://forum.ansible.com/t/merging-lists/37896/9 "2023-04-18T13:10:46Z")

</div>

**% cat foo.yml**

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 18, 2023, 2:40pm UTC](https://forum.ansible.com/t/merging-lists/37896/10 "2023-04-18T14:40:40Z")

</div>

Also, by using the sum() filter:

```auto
[utoddl@tango ansible]$ cat foo.yml
---
- name: test lists
  become: false
  gather_facts: false
  hosts: localhost
  vars:
    list_12:
      - { one: 'str1', two: 'str2' }
    list_34:
      - { three: 'str3', four: 'str4' }
    list_56:
      - { five: 'str5', six: 'str6' }
  tasks:
    - debug: msg="[{{ list_12, list_34, list_56 }}]"
    - debug: msg="{{ [list_12, list_34, list_56] | sum(start=[]) }}"

[utoddl@tango ansible]$ ansible-playbook foo.yml

PLAY [test lists] ***************************************************************

TASK [debug] ********************************************************************
ok: [localhost] => {
    "msg": [
        [
            [
                {
                    "one": "str1",
                    "two": "str2"
                }
            ],
            [
                {
                    "four": "str4",
                    "three": "str3"
                }
            ],
            [
                {
                    "five": "str5",
                    "six": "str6"
                }
            ]
        ]
    ]
}

TASK [debug] ********************************************************************
ok: [localhost] => {
    "msg": [
        {
            "one": "str1",
            "two": "str2"
        },
        {
            "four": "str4",
            "three": "str3"
        },
        {
            "five": "str5",
            "six": "str6"
        }
    ]
}

PLAY RECAP **********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

```

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 2:43pm UTC](https://forum.ansible.com/t/merging-lists/37896/11 "2023-04-18T14:43:01Z")

</div>

Brilliant!

Walter

---

<div class="post-metadata">

**Author:** ![mdidomenico](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mdidomenico](https://forum.ansible.com/u/mdidomenico)\
**Post date:** [April 18, 2023, 3:01pm UTC](https://forum.ansible.com/t/merging-lists/37896/12 "2023-04-18T15:01:40Z")

</div>

agreed, brillant. i'll give that a whirl, but it looks like exactly  
what i'm after. thanks

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 4:44pm UTC](https://forum.ansible.com/t/merging-lists/37896/13 "2023-04-18T16:44:03Z")

</div>

I just tested and this also works.

- debug: msg=“{{ [listb] | sum(start=lista) }}”

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 18, 2023, 4:46pm UTC](https://forum.ansible.com/t/merging-lists/37896/14 "2023-04-18T16:46:31Z")

</div>

And if you really wanted only unique list items this also works …

- debug: msg=“{{ [listb] | sum(start=lista) **| unique** }}”

Walter

---

<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:** [April 18, 2023, 5:12pm UTC](https://forum.ansible.com/t/merging-lists/37896/15 "2023-04-18T17:12:11Z")

</div>

Isn't \*flatten\* simpler?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ [list\_12, list\_34, list\_56] | flatten }}"
