# Multiple response from loop

**URL:** <https://forum.elixirforum.com/t/multiple-response-from-loop/48602>\
**Category:** Questions\
**Tags:** loop\
**Created:** [June 27, 2022, 9:49am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602 "2022-06-27T09:49:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasimbola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hasimbola/32/26622_2.png) [@Hasimbola](https://forum.elixirforum.com/u/Hasimbola)\
**Post date:** [June 27, 2022, 9:49am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/1 "2022-06-27T09:49:44Z")

</div>

Hello everybody,  
Someone can explain me this result please?  
I have 2 list  
x = [“0001”, “0002”]  
y = [1,1]

And I loop them like this:

> for x1 ← x do  
> for y1\<- y do  
> IO.inspect x1  
> IO.inspect y1  
> end  
> end

But the result is like this:

> “0001”  
> 1  
> “0001”  
> 1  
> “0002”  
> 1  
> “0002”  
> 1

But I want result like this :

> “0001”  
> 1  
> “0002”  
> 1  
> Thanks! 😃

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [June 27, 2022, 10:01am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/2 "2022-06-27T10:01:26Z")

</div>

Do you mean something like [`Enum.zip/2`](https://hexdocs.pm/elixir/master/Enum.html#zip/2)?

```elixir
for {x1, y1} <- Enum.zip(x, y) do

```

---

<div class="post-metadata">

**Author:** ![Hasimbola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hasimbola/32/26622_2.png) [@Hasimbola](https://forum.elixirforum.com/u/Hasimbola)\
**Post date:** [June 27, 2022, 10:03am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/3 "2022-06-27T10:03:38Z")

</div>

I want this result after loop:  
“0001”  
1  
“0002”  
1

but I receive:  
“0001”  
1  
“0001”  
1  
“0002”  
1  
“0002”  
1

the result is multiply by 2 , but I want also one result

---

<div class="post-metadata">

**Author:** ![Hasimbola](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hasimbola/32/26622_2.png) [@Hasimbola](https://forum.elixirforum.com/u/Hasimbola)\
**Post date:** [June 27, 2022, 10:07am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/4 "2022-06-27T10:07:01Z")

</div>

> [@sabiwara](#):
>
> ```elixir
> 
> ```

Thanks, it’s resolve my problem

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [June 27, 2022, 10:18am UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/5 "2022-06-27T10:18:42Z")

</div>

If `Enum.zip/2` solves your problem then consider using `[head | tail]` notation for lists like:

```elixir
defmodule Example do
  # when done or empty input do nothing returning empty list
  def sample([], []), do: []

  # optional code in case you have more items in one of the list
  # def sample([], _non_empty_list), do: []
  # def sample(_non_empty_list, []), do: []

  # loop using [head | tail] notation together with your code
  def sample([head1 | tail1], [head2 | tail2]) do
    IO.puts(head1)
    IO.puts(head2)
    sample(tail1, tail2)
  end
end

x = ["0001", "0002"]
y = [1, 1]
Example.sample(x, y)

```

This gave you exactly same result, but … `Enum.zip` is one **loop** and `for` is **another** one - this means that if you write a bit more code then using just one loop your code would be faster.

---

<div class="post-metadata">

**Author:** ![sabiwara](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sabiwara/32/18612_2.png) [@sabiwara](https://forum.elixirforum.com/u/sabiwara)\
**Post date:** [June 27, 2022, 10:36pm UTC](https://forum.elixirforum.com/t/multiple-response-from-loop/48602/6 "2022-06-27T22:36:17Z")

</div>

Indeed, it depends on the actual use case and what we want to return.

We might also want to reach for [`Enum.zip_reduce/4`](https://hexdocs.pm/elixir/Enum.html#zip_reduce/4) (for an arbitrary accumulator) or [`Enum.zip_with/3`](https://hexdocs.pm/elixir/Enum.html#zip_with/3) (if we want to return a list).

```elixir
Enum.zip_reduce(x, y, :ok, fn x1, y1, _acc ->
  IO.puts(x1)
  IO.puts(y1)
end)

```
