# How to loop over list while pulling data from different list?

**URL:** <https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668>\
**Category:** Ansible Project\
**Created:** [March 4, 2015, 10:03pm UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668 "2015-03-04T22:03:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack.martin8912](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jack.martin8912](https://forum.ansible.com/u/jack.martin8912)\
**Post date:** [March 4, 2015, 10:03pm UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/1 "2015-03-04T22:03:15Z")

</div>

Hello,

I’m encountering a loop issue that I’m unsure how to solve cleanly. Suppose I have a list of dictionaries:

list\_of\_dict:

- { key: d01, value: ‘v1’ }
- { key: d02, value: ‘v2’ }  
…
- { key: d99, value: ‘v99’ }

Somewhere else I have another list whose elements are a subset of the keys in list\_of\_dict:

list01:

- d01

- d07

- d33

There are many other lists like list01 with a similar structure. How do I loop through list01 while echoing the corresponding ‘value’ from list\_of\_dict?

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [March 4, 2015, 11:58pm UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/2 "2015-03-04T23:58:58Z")

</div>

Jack,

Interesting dilemma, I believe a little bit of Jinja2 would help you out. Try running this:

- name: Test playbook  
hosts: localhost  
vars:

list01:

- d01
- d03
- d04
- d06  
list02:
- d02
- d05
- d06  
list\_of\_dict:
- { key: d01, value: ‘v1’ }
- { key: d02, value: ‘v2’ }
- { key: d03, value: ‘v3’ }
- { key: d04, value: ‘v4’ }
- { key: d05, value: ‘v5’ }
- { key: d06, value: ‘v6’ }  
tasks:
- debug: msg=“{% for each in list\_of\_dict %}{{ each.value if each.key == item else ‘’ }}{% endfor %}”

with\_items:

- “{{list01}}”
- “{{list02}}”

This seems easily scalable and works like a charm!

\Chip

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 5, 2015, 12:03am UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/3 "2015-03-05T00:03:42Z")

</div>

have you tried:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ llist\_of\_dict[item] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:"{{list01|union(list02)}}"

---

<div class="post-metadata">

**Author:** ![jack.martin8912](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jack.martin8912](https://forum.ansible.com/u/jack.martin8912)\
**Post date:** [March 6, 2015, 4:45am UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/4 "2015-03-06T04:45:54Z")

</div>

That is exactly what I was looking for! Thanks.

---

<div class="post-metadata">

**Author:** ![Clay\_Ye](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@Clay\_Ye](https://forum.ansible.com/u/Clay_Ye)\
**Post date:** [March 6, 2015, 7:25am UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/5 "2015-03-06T07:25:28Z")

</div>

the above code got this error message, how to fix it ?

TASK: [debug msg=“{{ list\_of\_dict[item] }}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost] =\> One or more undefined variables: ‘list object’ has no attribute ‘d01’

FATAL: all hosts have already failed – aborting

---

<div class="post-metadata">

**Author:** ![jack.martin8912](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jack.martin8912](https://forum.ansible.com/u/jack.martin8912)\
**Post date:** [March 6, 2015, 4:49pm UTC](https://forum.ansible.com/t/how-to-loop-over-list-while-pulling-data-from-different-list/17668/6 "2015-03-06T16:49:16Z")

</div>

Hi Clay,

My understanding is that list\_of\_dict, being a list, should be indexed by integer and not by a string such as ‘d01’. Try the code in Chip’s response; it runs successfully.
