# List challenge

**URL:** <https://discourse.julialang.org/t/list-challenge/43447>\
**Category:** New to Julia\
**Tags:** stats\
**Created:** [July 21, 2020, 8:43pm UTC](https://discourse.julialang.org/t/list-challenge/43447 "2020-07-21T20:43:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [July 21, 2020, 8:43pm UTC](https://discourse.julialang.org/t/list-challenge/43447/1 "2020-07-21T20:43:39Z")

</div>

I have a list: `my_list=collect(1:n)', and a repeatability vector: `repeat\_list = [n1, n1, n2, n1, n2, n2]`. I would like to create a new list with ` repeated `n1` times, 2 repeated `n1` times, 3 repeated `n2` times, etc. The length of the list could be a few hundred thousand. Is there a known “best” way to accomplish this? In the meantime, I keep searching. Thanks.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [July 21, 2020, 8:57pm UTC](https://discourse.julialang.org/t/list-challenge/43447/2 "2020-07-21T20:57:04Z")

</div>

`reduce(append!, Iterators.repeated.(eachindex(repeat_list), repeat_list), init=Int[])`?

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [July 21, 2020, 9:01pm UTC](https://discourse.julialang.org/t/list-challenge/43447/3 "2020-07-21T21:01:34Z")

</div>

Something like this?

```julia
[fill(a,n) for (a,n) in zip(my_list, repeat_list)]

```

(I did not test is, but will do tomorrow 😄 )

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [July 22, 2020, 2:56am UTC](https://discourse.julialang.org/t/list-challenge/43447/4 "2020-07-22T02:56:40Z")

</div>

What you are referring to is [run-length encoding](https://en.wikipedia.org/wiki/Run-length_encoding). Or rather, what you start out with is run-length encoding, and you want to do inverse run-length encoding. The [`StatsBase.jl`](https://juliastats.org/StatsBase.jl/stable/) package has an `rle` function and an `inverse_rle` function. The `inverse_rle` function does what you’re looking for:

```julia
julia> inverse_rle([5, 3, 1], [1, 2, 3])
6-element Array{Int64,1}:
 5
 3
 3
 1
 1
 1

```

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [July 22, 2020, 3:07am UTC](https://discourse.julialang.org/t/list-challenge/43447/5 "2020-07-22T03:07:22Z")

</div>

Thank you all for your answers and ideas, which I will try out. I can now tell you that the case I was actually working with (for now) involved only two numbers separated by unity. Thus, given a sequence: `seq=collect(1:100)` along with a sequence `seq2` with a mixture of 5 and 6’s? I wanted to create a new sequence with the numbers in `seq` repeated according to the information in `seq2`. This was easily accomplished with two conditional statement and `hcat`. I wonder how efficient my implementation is compared to the ideas offered above. Thanks again! I have learned a lot!

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [July 22, 2020, 10:32am UTC](https://discourse.julialang.org/t/list-challenge/43447/6 "2020-07-22T10:32:25Z")

</div>

This should be `foldl` rather than `reduce`, since the operation is not associative. The fact that `reduce` happens to work in this case is due to an implementation detail (that by default, `reduce` is implemented as `foldl`)

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [July 22, 2020, 11:41am UTC](https://discourse.julialang.org/t/list-challenge/43447/7 "2020-07-22T11:41:05Z")

</div>

Agreed.  
Still, I am amused by how nicely the return values of `push!` and `append!` fit together with the fold concept.
