# format List comparison ansible

**URL:** <https://forum.ansible.com/t/format-list-comparison-ansible/32097>\
**Category:** Ansible Project\
**Created:** [January 30, 2020, 12:49pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097 "2020-01-30T12:49:02Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 30, 2020, 12:49pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/1 "2020-01-30T12:49:02Z")

</div>

Hi ,

I want to compare two lists . But while doing this the list1 item loops over each item of list2. I just want item of list 1 just to compare between matching item in list2 and not with others.

my playbook:

- hosts: localhost  
tasks:

- name: Create a list1  
set\_fact:  
list1:

- dp-steps-common-3.0.12

- iam-python-common-1.0.1

- iam-service-default-3.1.37  
list2:

- dp-steps-common-3.0.15-1911280809\_d103a.noarch

- iam-python-common-1.0.1-1910242116.noarch

- iam-service-default-3.1.37-1911141021\_63e48.noarch

- name: If the List comparision is equal

debug:  
msg: " {{ item[0] }} is equal to {{ item[1] }}"  
with\_nested:

- “{{ list1 }}”

- “{{ list2 }}”  
when: (item[0] in item[1])

- name: If the List comparision is not equal  
debug:  
msg: " {{ item[0] }} is not equal to {{ item[1] }}"  
with\_nested:

- “{{ list1 }}”

- “{{ list2 }}”

# when: (item[0] not in item[1])

when: list1 | difference(list2)

o/p:

TASK [If the List comparision is equal] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’])  
skipping: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’iam-python-common-1.0.1-1910242116.noarch’])  
skipping: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’])  
skipping: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’])  
ok: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’iam-python-common-1.0.1-1910242116.noarch’]) =\> {  
“msg”: " iam-python-common-1.0.1 is equal to iam-python-common-1.0.1-1910242116.noarch"  
}  
skipping: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’])  
skipping: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’])  
skipping: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’iam-python-common-1.0.1-1910242116.noarch’])  
ok: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’]) =\> {  
“msg”: " iam-service-default-3.1.37 is equal to iam-service-default-3.1.37-1911141021\_63e48.noarch"  
}

TASK [If the List comparision is not equal] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’]) =\> {  
“msg”: " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
}  
ok: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’iam-python-common-1.0.1-1910242116.noarch’]) =\> {  
“msg”: " dp-steps-common-3.0.12 is not equal to iam-python-common-1.0.1-1910242116.noarch"  
}  
ok: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’]) =\> {  
“msg”: " dp-steps-common-3.0.12 is not equal to iam-service-default-3.1.37-1911141021\_63e48.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’]) =\> {  
“msg”: " iam-python-common-1.0.1 is not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’iam-python-common-1.0.1-1910242116.noarch’]) =\> {  
“msg”: " iam-python-common-1.0.1 is not equal to iam-python-common-1.0.1-1910242116.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-python-common-1.0.1’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’]) =\> {  
“msg”: " iam-python-common-1.0.1 is not equal to iam-service-default-3.1.37-1911141021\_63e48.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’]) =\> {  
“msg”: " iam-service-default-3.1.37 is not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’iam-python-common-1.0.1-1910242116.noarch’]) =\> {  
“msg”: " iam-service-default-3.1.37 is not equal to iam-python-common-1.0.1-1910242116.noarch"  
}  
ok: [localhost] =\> (item=[u’iam-service-default-3.1.37’, u’iam-service-default-3.1.37-1911141021\_63e48.noarch’]) =\> {  
“msg”: " iam-service-default-3.1.37 is not equal to iam-service-default-3.1.37-1911141021\_63e48.noarch"  
}

Desired o/p of last task:

ok: [localhost] =\> (item=[u’dp-steps-common-3.0.12’, u’dp-steps-common-3.0.15-1911280809\_d103a.noarch’]) =\> {  
“msg”: " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch"

---

<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:** [January 30, 2020, 1:50pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/2 "2020-01-30T13:50:45Z")

</div>

Is this what you're looking for?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item }} is in the list2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: list2|select('match', item)|list

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-python-common-1.0.1 is in the list2"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-service-default-3.1.37 is in the list2

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 30, 2020, 2:11pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/3 "2020-01-30T14:11:34Z")

</div>

HI Vladimir,

Thanksfor your reply.

Actually this is not what i want , i just want my item1 to not to iterate over the loop and compare every item, it should only compare the relative item2.  
PLs check my o/p.  
I just only want the o/p:  
“msg”: " dp-steps-common-3.0.12 is not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch"

Regards  
Rakesh

---

<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:** [January 30, 2020, 3:17pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/4 "2020-01-30T15:17:17Z")

</div>

OK. Is this probably what you're looking for?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.0 }} is not equal to {{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1|zip(list2)|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])

gives

&nbsp;&nbsp;&nbsp;"msg": "dp-steps-common-3.0.12 is not equal to  
&nbsp;&nbsp;&nbsp;dp-steps-common-3.0.15-1911280809\_d103a.noarch"

The order and the format of the items is significant. HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 30, 2020, 6:08pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/5 "2020-01-30T18:08:02Z")

</div>

Hi Vladimir,

I have another query . Now i altered the sequence of items in the lists, the iam python of list1 compares with the dp-steps of list

Is there any way the list comparision would be successful even if the sequence is altered.

list1:

- iam-python-common-1.0.0
- iam-service-default-3.1.42
- dp-steps-common-3.0.12  
list2:
- dp-steps-common-3.0.15-1911280809\_d103a.noarch
- iam-service-default-3.1.37-1911141021\_63e48.noarch
- iam-python-common-1.0.1-1910242116.noarch

---

<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:** [January 30, 2020, 6:29pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/6 "2020-01-30T18:29:18Z")

</div>

Sure. Sort the lists. For example

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.0 }} is not equal to {{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1|sort|zip(list2|sort)|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.0.split('-')[-1] is not version(item.1.split('-')[-2])

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "dp-steps-common-3.0.12 is not equal to  
&nbsp;&nbsp;&nbsp;&nbsp;dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-python-common-1.0.0 is not equal to  
&nbsp;&nbsp;&nbsp;&nbsp;iam-python-common-1.0.1-1910242116.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-service-default-3.1.42 is not equal to  
&nbsp;&nbsp;&nbsp;&nbsp;iam-service-default-3.1.37-1911141021\_63e48.noarch"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [January 30, 2020, 6:40pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/7 "2020-01-30T18:40:57Z")

</div>

Hi Vladimir,

Thanks for the reply. Awsome.  
Its exactly what i want to achieve. Thanks for your time . Highly appreciate your help:)

With Regards  
Rakesh

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [January 31, 2020, 4:20am UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/8 "2020-01-31T04:20:53Z")

</div>

Thanks Vladimir ,

Extraordinary solution .  
Just for curiosity , would be please explain the steps in solution .

warm regards  
Pawan

---

<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:** [January 31, 2020, 8:20am UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/9 "2020-01-31T08:20:04Z")

</div>

Sure. The steps are self-explaining, I think.

1) See "zip"  
[https://docs.ansible.com/ansible/devel/user\_guide/playbooks\_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest](https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#combining-items-from-multiple-lists-zip-and-zip-longest)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.0 }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1|sort|zip(list2|sort)|list }}"

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "dp-steps-common-3.0.12 dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-python-common-1.0.0 iam-python-common-1.0.1-1910242116.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-service-default-3.1.42 iam-service-default-3.1.37-1911141021\_63e48.noarch"

2) See "split" and "Common Sequence Operations"  
[https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split](https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split)  
[https://docs.python.org/3/library/stdtypes.html?highlight=split#common-sequence-operations](https://docs.python.org/3/library/stdtypes.html?highlight=split#common-sequence-operations)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.0.split('-')[-1] }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.1.split('-')[-2] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1|sort|zip(list2|sort)|list }}"

gives

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "3.0.12 3.0.15"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "1.0.0 1.0.1"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "3.1.42 3.1.37"

3) See "Version Comparison"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#version-comparison](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#version-comparison)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 6, 2020, 8:42am UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/10 "2020-02-06T08:42:28Z")

</div>

Hi Vladimir,

If my items of lists are not fixed on both the list , how do i compare them:

Now i have one more issue on the LIST comparison:  
If the numbers of items in list is not equal is there way to compare:

suppose my lists are:  
list1

“dp-steps-common-3.0.12”,  
“iam-python-common-1.0.1”,  
“iam-service-default-3.1.37”  
list2:  
“dp-steps-common-3.0.15-1911280809\_d103a.noarch”,  
“iam-service-default-3.1.37-1911141021\_63e48.noarch”

- name: If the List comparision is not equal  
debug:  
msg: “{{ item.0 }} is not equal to {{ item.1 }}”  
loop: “{{ list1|sort|zip(list2|sort)|list }}”  
when: item.0.split(‘-’)[-1] is not version(item.1.split(‘-’)[-2])

As you can see the items are not same in both list.

---

<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:** [February 6, 2020, 10:55am UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/11 "2020-02-06T10:55:52Z")

</div>

Do you know what you would like to get as a result?

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 6, 2020, 11:08am UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/12 "2020-02-06T11:08:24Z")

</div>

Hi Vladimir,

I would like my o/p as below:

dp-steps-common-3.0.12 not equal to dp-steps-common-3.0.15-1911280809\_d103a.noarch

iam-service-default-3.1.37 is equal to iam-service-default-3.1.37-1911141021\_63e48.noarch

iam-python-common-1.0.1 not found in list2

---

<div class="post-metadata">

**Author:** ![vishwas\_govekar1](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@vishwas\_govekar1](https://forum.ansible.com/u/vishwas_govekar1)\
**Post date:** [February 6, 2020, 1:36pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/13 "2020-02-06T13:36:05Z")

</div>

I am working on Apache storm project using Ansible scripts ,I have stormconfproperties.yml which has variable values for supervisor\_slots\_ports

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b3d6681140d9a3b8051885f915258999efb99898.png)

In my template files I have to pass it as variables .Can you help me for supervisor.slots.ports as I have done for the remaining but unable to do that  
I have tried several times but failed

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9d574bd4dc05288bdd214880ea0ba353aa9bfe1.png)

---

<div class="post-metadata">

**Author:** ![vishwas\_govekar1](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@vishwas\_govekar1](https://forum.ansible.com/u/vishwas_govekar1)\
**Post date:** [February 6, 2020, 2:29pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/14 "2020-02-06T14:29:55Z")

</div>

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b3d6681140d9a3b8051885f915258999efb99898.png)  
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9d574bd4dc05288bdd214880ea0ba353aa9bfe1.png)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 6, 2020, 2:56pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/15 "2020-02-06T14:56:26Z")

</div>

Hi

You already are engaged in a conversation on this list. Branching that into another by verbatim forwarding a single message without context isn’t going to help you get better responses.

Please post proper questions, or just wait until the volunteers here respond to your original question.

FYI this is a best effort list, no SLA etc.

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9d574bd4dc05288bdd214880ea0ba353aa9bfe1.png)  
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b3d6681140d9a3b8051885f915258999efb99898.png)

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 6, 2020, 3:07pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/16 "2020-02-06T15:07:37Z")

</div>

Hi Vladimir,  
I have tried a lot but unable to find solution for my issue. Pls help on this

---

<div class="post-metadata">

**Author:** ![vishwas\_govekar1](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@vishwas\_govekar1](https://forum.ansible.com/u/vishwas_govekar1)\
**Post date:** [February 6, 2020, 3:41pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/17 "2020-02-06T15:41:53Z")

</div>

Thanks for the response

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9d574bd4dc05288bdd214880ea0ba353aa9bfe1.png)  
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b3d6681140d9a3b8051885f915258999efb99898.png)

---

<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:** [February 6, 2020, 3:57pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/18 "2020-02-06T15:57:04Z")

</div>

Sure. Transform the data first. For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_pkgs: "{{ my\_pkgs|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({pkg\_name: {'list1': item, 'list2': pkg2}}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pkg\_name: "{{ item.split('-')[:-1]|join('-') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pkg2: "{{ list2|select('search', pkg\_name)|list|first|default('') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_pkgs

give

&nbsp;&nbsp;&nbsp;&nbsp;"my\_pkgs": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"dp-steps-common": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list1": "dp-steps-common-3.0.12",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list2": "dp-steps-common-3.0.15-1911280809\_d103a.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"iam-python-common": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list1": "iam-python-common-1.0.1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list2": ""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"iam-service-default": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list1": "iam-service-default-3.1.37",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"list2": "iam-service-default-3.1.37-1911141021\_63e48.noarch"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}

Then the manipulation with the data is trivial. For example

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if item.value.list2 %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if item.value.list1 in item.value.list2 %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.value.list1 }} is equal to {{ item.value.list2 }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.value.list1 }} not equal to {{ item.value.list2 }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.value.list1 }} not found in list2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_pkgs|dict2items }}"

gives you what you want

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-python-common-1.0.1 not found in list2\n"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "iam-service-default-3.1.37 is equal to  
&nbsp;&nbsp;&nbsp;&nbsp;iam-service-default-3.1.37-1911141021\_63e48.noarch\n"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "dp-steps-common-3.0.12 not equal to  
&nbsp;&nbsp;&nbsp;&nbsp;dp-steps-common-3.0.15-1911280809\_d103a.noarch\n"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [February 6, 2020, 10:42pm UTC](https://forum.ansible.com/t/format-list-comparison-ansible/32097/19 "2020-02-06T22:42:38Z")

</div>

Hi Vladimir,  
This Solutioñ works.  
Thanks for your extended help on this.  
I just tweaked something to match my requirements, overall your solution works well for me.  
Regards  
Rakesh
