# How to nest any number of for loops

**URL:** https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067
**Category:** General Usage
**Tags:** iterators
**Created:** [May 9, 2024, 8:17pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067 "2024-05-09T20:17:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)
#### Post date: [May 9, 2024, 8:17pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/1 "2024-05-09T20:17:52Z")

</div>

I need to iterate over all combinations of k permutations of integers (1:n).

Say, k=2 and n=2.  
The permutations of (1:2) are  
[1, 2]  
[2, 1]

and what i need is  
[1, 2] , [1, 2]  
[1, 2] , [2, 1]

[2, 1] , [1, 2]  
[2, 1] , [2, 1]

In this case (k=2) i can obtain the iterator (using [Combinatorics.jl](https://github.com/JuliaMath/Combinatorics.jl)) such as (MWE)

```julia
using Combinatorics
n=2
# k=2, 2 nested for loops
P=((a, b) for a in permutations(1:n) for b in permutations(1:n))
foreach(println, P)

```

How can i generalize this for any positive k?

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [May 9, 2024, 8:30pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/2 "2024-05-09T20:30:52Z")

</div>

My first guess would be to go to [`Base.Cartesian`](https://docs.julialang.org/en/v1/devdocs/cartesian/) looking for the right macro, these macros are used to construct an arbitrary number of nested loops at compile time. But here maybe you simply need somethign recursive alike the following:

```julia
function make(k,n)
    if k == 0
        return 1:n
    else
        return permutations(make(k-1,n))
    end
end

```

?

* * *

EDIT: by the way,

> [@Marco-Congedo](#):
>
> iterate over all combinations of k permutations of integers (1:n)

does not correspond to what your code does for k=2, which iterates over permutation(1:n) x permutations(1:n) ? Perhaps you could clarify a bit what you need ?

* * *

EDIT: I _think_ I understood what you want, which could be this:

```julia
using Base.Iterators
k=2
n=2
for indices in product((permutations(1:n) for i in 1:k)...)
    @show indices
end

```

which outputs

```julia
indices = ([1, 2], [1, 2])
indices = ([2, 1], [1, 2])
indices = ([1, 2], [2, 1])
indices = ([2, 1], [2, 1])

```

?

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [May 9, 2024, 8:32pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/3 "2024-05-09T20:32:54Z")

</div>

> [@lrnv](#):
>
> My first guess would be to go to [`Base.Cartesian`](https://docs.julialang.org/en/v1/devdocs/cartesian/) looking for the right macro, these macros are used to construct an arbitrary number of nested loops at compile time.

The Base.Cartesian.@nloops macro constructs such things.  
Take a look at [Base.Cartesian · The Julia Language](https://docs.julialang.org/en/v1/devdocs/cartesian/)

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 9, 2024, 9:03pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/4 "2024-05-09T21:03:56Z")

</div>

Not beautiful, but does what you ask.

```julia
f(n,k)=collect(multiset_permutations(repeat(collect(permutations(1:n)),k),k))

```

---

<div class="post-metadata">

### Author: ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)
#### Post date: [May 9, 2024, 9:29pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/5 "2024-05-09T21:29:49Z")

</div>

Thanks, this works indeed:

```julia
using Combinatorics
n=3
k=2
P=Iterators.product((permutations(1:k) for i in 1:n)...) 
foreach(println, P)

```

---

<div class="post-metadata">

### Author: ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)
#### Post date: [May 9, 2024, 9:40pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/6 "2024-05-09T21:40:16Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `f(n,k)=collect(multiset_permutations(repeat(collect(permutations(1:n)),k),k))`

This works, but creates physically the permutations as a vector. I just need an iterator.

---

<div class="post-metadata">

### Author: ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)
#### Post date: [May 9, 2024, 9:41pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/7 "2024-05-09T21:41:16Z")

</div>

Good to know! i’ll check if needed

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 9, 2024, 9:43pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/8 "2024-05-09T21:43:23Z")

</div>

```julia

f(n,k)=multiset_permutations(repeat([permutations(1:n)...],k),k)

k=2
n=2
for p in f(n,k)
    @show p
end

```

---

<div class="post-metadata">

### Author: ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)
#### Post date: [May 9, 2024, 10:14pm UTC](https://discourse.julialang.org/t/how-to-nest-any-number-of-for-loops/114067/9 "2024-05-09T22:14:32Z")

</div>

Thanks, this does the job. Just, as i need the (k!)^n permutations, the `k` and `n` indices must be interchanged, as for the solution given by @irvn (my bad, my question was not correct). This is what i’ve got:

```julia
k=2
n=3
num_of_permutations=(factorial(k)^n)
P = multiset_permutations(repeat([permutations(1:k)...], n) , n)
foreach(println, P)

```
