# Iterator from 3 vectors

**URL:** <https://discourse.julialang.org/t/iterator-from-3-vectors/101751>\
**Category:** New to Julia\
**Tags:** question, package, iterators\
**Created:** [July 18, 2023, 8:38pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751 "2023-07-18T20:38:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuri\_Masakazu\_Mizusa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuri_masakazu_mizusa/32/36504_2.png) [@Yuri\_Masakazu\_Mizusa](https://discourse.julialang.org/u/Yuri_Masakazu_Mizusa)\
**Post date:** [July 18, 2023, 8:38pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/1 "2023-07-18T20:38:33Z")

</div>

Hi,  
I’m trying to create an iterator that does the following:

```julia
vec_coord_x=[1,2,3]
vec_coord_y=[1,2,3]
vec_coord_z=[1,2,3]

[(x,y,z) for x in vec_coord_x for y in vec_coord_y for z in vec_coord_z]

```

Resulting in:  
27-element Vector{Tuple{Int64, Int64, Int64}}:  
(1, 1, 1)  
(1, 1, 2)  
(1, 1, 3)  
(1, 2, 1)  
(1, 2, 2)  
(1, 2, 3)  
⋮  
(3, 2, 2)  
(3, 2, 3)  
(3, 3, 1)  
(3, 3, 2)  
(3, 3, 3)

I can use the for loop, but I think it would be faster with the iterator, correct?  
Is there a function in the pack “IterTools” that do this?  
Thank you!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [July 18, 2023, 8:48pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/2 "2023-07-18T20:48:28Z")

</div>

The generator expression is optimal itself, just use parentheses rather than square brackets to avoid collecting the elements.

---

<div class="post-metadata">

**Author:** ![Yuri\_Masakazu\_Mizusa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuri_masakazu_mizusa/32/36504_2.png) [@Yuri\_Masakazu\_Mizusa](https://discourse.julialang.org/u/Yuri_Masakazu_Mizusa)\
**Post date:** [July 18, 2023, 8:48pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/3 "2023-07-18T20:48:39Z")

</div>

I think I found it, I was not just wording the search right.

```julia
it_1=Iterators.product(vec_coord_x,vec_coord_y,vec_coord_z)
for (a,b,c) in it_1
    println(a,b,c)
end

```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [July 18, 2023, 8:50pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/4 "2023-07-18T20:50:46Z")

</div>

I’m told that Julia stands for “Just using loops is amazing” or something like that. 🙂 … although after many years of Python, I like iterators a lot.

---

<div class="post-metadata">

**Author:** ![Yuri\_Masakazu\_Mizusa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuri_masakazu_mizusa/32/36504_2.png) [@Yuri\_Masakazu\_Mizusa](https://discourse.julialang.org/u/Yuri_Masakazu_Mizusa)\
**Post date:** [July 18, 2023, 8:52pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/5 "2023-07-18T20:52:32Z")

</div>

Hi!  
I found the code `Iterators.product(vec_coord_x,vec_coord_y,vec_coord_z)` does that.  
Do you think it would be better to change back to what I was using? Or is the same?  
Thanks for the help!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [July 18, 2023, 9:38pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751/6 "2023-07-18T21:38:02Z")

</div>

In theory there shouldn’t be a big difference, but I won’t swear on it. If one is better performance it’s probably the `product`. But you can check by `using BemchmarkTools; @btime your_function()` with a function that creates the iterator and realistically uses elements from it.
