# Possible approaches to iteration

**URL:** <https://forum.ansible.com/t/possible-approaches-to-iteration/27227>\
**Category:** Ansible Project\
**Created:** [April 9, 2018, 1:45pm UTC](https://forum.ansible.com/t/possible-approaches-to-iteration/27227 "2018-04-09T13:45:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom.reynick](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@tom.reynick](https://forum.ansible.com/u/tom.reynick)\
**Post date:** [April 9, 2018, 1:45pm UTC](https://forum.ansible.com/t/possible-approaches-to-iteration/27227/1 "2018-04-09T13:45:59Z")

</div>

I would like to understand semantics of `with_items` in ansible and compare this approach to `map` for example.

For example, let consider:

`{{ ["1","2", "3"] | map('regex_replace', '^(.*)$', \1'some tail') tail }}`

How to reach the same effect using `with_items` ?  
And in general how does it work ? Where is the body of this loop ?  
For example (got from some stack overflow thread):

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 11, 2018, 3:00am UTC](https://forum.ansible.com/t/possible-approaches-to-iteration/27227/2 "2018-04-11T03:00:56Z")

</div>

In the case of 'set\_fact' you should not see much difference, but in  
most other cases map will always be faster, with\_\<lookup\> executes the  
task once per item, except when squashing, but that is magic reserved  
to only a few plugins.

FYI the 'item's part is equivalent of loop: '{{ [ "four", "five",  
"six"] | flatten(depth=1) }}', a closer representation to map would be  
with\_list.
