# Iterate through all combinations of elements in an arbitrary number of arrays

**URL:** https://discourse.julialang.org/t/iterate-through-all-combinations-of-elements-in-an-arbitrary-number-of-arrays/68819
**Category:** General Usage
**Tags:** first-steps, array, iterators
**Created:** [September 27, 2021, 4:47pm UTC](https://discourse.julialang.org/t/iterate-through-all-combinations-of-elements-in-an-arbitrary-number-of-arrays/68819 "2021-09-27T16:47:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [September 27, 2021, 4:47pm UTC](https://discourse.julialang.org/t/iterate-through-all-combinations-of-elements-in-an-arbitrary-number-of-arrays/68819/1 "2021-09-27T16:47:12Z")

</div>

I have an array `arrays` which contains a number of arrays (arbitrary). How do I iterate through all combinations of elelements? If I know that there are e.g. 3 arrays I can do:

```julia
for e1 in arrays[1], e2 in arrays[2], e3 in arrays[3]
    do_stuff([e1,e2,e3])
end

```

but if the number is arbitrary, what do I do then?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [September 27, 2021, 4:50pm UTC](https://discourse.julialang.org/t/iterate-through-all-combinations-of-elements-in-an-arbitrary-number-of-arrays/68819/2 "2021-09-27T16:50:37Z")

</div>

You can use `Iterators.product`:

```julia
for elements in Iterators.product(arrays...)
  do_stuff(elements)
end

```

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [September 27, 2021, 4:53pm UTC](https://discourse.julialang.org/t/iterate-through-all-combinations-of-elements-in-an-arbitrary-number-of-arrays/68819/3 "2021-09-27T16:53:29Z")

</div>

That worked perfectly, loads of thanks 🙂
