# How to do permutations with repetition?

**URL:** <https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192>\
**Category:** General Usage\
**Created:** [January 19, 2023, 5:52am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192 "2023-01-19T05:52:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [January 19, 2023, 5:52am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/1 "2023-01-19T05:52:00Z")

</div>

Hi there,

Looking to get some ideas on the best approach to doing permutations with repetition please. If I have an array with {x,y,z,…} distinct elements and want to generate all permutations to a length of N allowing repetition, how would I be best to go about doing this?

Thanks

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 19, 2023, 6:51am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/2 "2023-01-19T06:51:12Z")

</div>

Are you looking for a product iterator?

```julia
julia> vec(collect(Base.Iterators.product(Base.Iterators.repeated(1:3, 3)...)))
27-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (2, 1, 1)
 (3, 1, 1)
 (1, 2, 1)
 (2, 2, 1)
 (3, 2, 1)
 (1, 3, 1)
 (2, 3, 1)
 (3, 3, 1)
 (1, 1, 2)
 (2, 1, 2)
 (3, 1, 2)
 (1, 2, 2)
 (2, 2, 2)
 (3, 2, 2)
 (1, 3, 2)
 (2, 3, 2)
 (3, 3, 2)
 (1, 1, 3)
 (2, 1, 3)
 (3, 1, 3)
 (1, 2, 3)
 (2, 2, 3)
 (3, 2, 3)
 (1, 3, 3)
 (2, 3, 3)
 (3, 3, 3)

```

---

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [January 19, 2023, 7:03am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/3 "2023-01-19T07:03:35Z")

</div>

This appears to be exactly what I need, thanks.

The “…” notation threw me off a bit because I haven’t seen it in a programming language before I don’t think, but I’ve given the command a run and it appears to be working for me.

Thanks for the answer

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [January 19, 2023, 7:46am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/4 "2023-01-19T07:46:18Z")

</div>

By the way, `Iterators` is exported, so you can write `Iterators.product` instead of `Base.Iterators.product`, etc.

Another perspective is that this is like counting in base three:

```julia
julia> [digits(x - 1, base=3, pad=3) .+ 1 for x in 1:3^3]
27-element Vector{Vector{Int64}}:
 [1, 1, 1]
 [2, 1, 1]
 [3, 1, 1]
 [1, 2, 1]
 [2, 2, 1]
 [3, 2, 1]
 [1, 3, 1]
 [2, 3, 1]
 [3, 3, 1]
 [1, 1, 2]
 ⋮
 [3, 3, 2]
 [1, 1, 3]
 [2, 1, 3]
 [3, 1, 3]
 [1, 2, 3]
 [2, 2, 3]
 [3, 2, 3]
 [1, 3, 3]
 [2, 3, 3]
 [3, 3, 3]

```

---

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [January 20, 2023, 1:58am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/5 "2023-01-20T01:58:43Z")

</div>

vec(collect(Base.Iterators.product(Base.Iterators.repeated(1:3, 3)…)))

Is there any way to modify the above code to return the result in a Vector instead of a Tuple?

The way I use the code looks like such:

```julia
arr_values = [0.6, 2.3] 
result = vec(collect(Iterators.product(Iterators.repeated(arr_values, length(arr))...)))

```

Alternatively, could the other syntax you shared ( [digits(x - 1, base=3, pad=3) .+ 1 for x in 1:3^3] ), be modified to support me using my arr\_values for the permutations instead of a straight order of consecutive numbers like 1:3. I’ve got the below code and it’s giving me an “ERROR: syntax: invalid iteration specification”:

```julia
arr_values = [0.6,2.3] 
res = [digits(x-1, base=3, pad=3) for .+ 1 for x in (arr_values)^3]

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 20, 2023, 4:34am UTC](https://discourse.julialang.org/t/how-to-do-permutations-with-repetition/93192/6 "2023-01-20T04:34:32Z")

</div>

Is there a reason `vec` doesn’t have a method operating on `Tuple`s?

```julia
Base.vec(t::Tuple) = [t...]
vec(map(vec, Iterators.product(Iterators.repeated(1:3, 3)...)))

```
