# Multiple response from loop

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! Do you mean something like `Enum.zip/2`?

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

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

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

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

Thanks, it’s resolve my problem

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

``````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: []

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.

2 Likes

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` (for an arbitrary accumulator) or `Enum.zip_with/3` (if we want to return a list).

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