# How to create a vector of variable length in julia?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060>\
**Category:** General Usage\
**Created:** [January 24, 2019, 8:15pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060 "2019-01-24T20:15:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 24, 2019, 8:15pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/1 "2019-01-24T20:15:22Z")

</div>

I’d like to create a vector and then add value to it one by one. The thing is, I do not know how many values I’d like to add in total until the process is done. Is this doable using julia vector? Or do I need another data structure instead? Many thanks.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 24, 2019, 8:18pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/2 "2019-01-24T20:18:29Z")

</div>

```julia
julia> v = Int[]
0-element Array{Int64,1}

julia> push!(v, 42)
1-element Array{Int64,1}:
 42

julia> push!(v, 3)
2-element Array{Int64,1}:
 42
  3

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 24, 2019, 8:21pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/3 "2019-01-24T20:21:19Z")

</div>

@bennedich, thank you. `pop!` occurs in my mind after I post this topic. 😉 Then there must be the other operation called `push!`.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 24, 2019, 10:11pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/4 "2019-01-24T22:11:13Z")

</div>

Is this an efficient operation? Am I correct in that by pushing to a vector, the compiler has to allocate new space, and copy the old vector over?  
I am sure for small vectors this is a non issue, but what happens when your vector is expected to grow large? Would it allocate/copy everytime?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 24, 2019, 10:24pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/5 "2019-01-24T22:24:50Z")

</div>

Vectors are by default variable length in Julia. If you want fixed length, you should use StaticArrays or perhaps tuples. They are not very useful for large sizes, though.

I believe that when you repeatedly `push!`, extra space is allocated, something like doubling (or 1.5x?) the buffer each time you overflow the old. You can also use `sizehint!` if you have a rough idea what the final size will be.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 24, 2019, 10:34pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/6 "2019-01-24T22:34:32Z")

</div>

> [@DNF](#):
>
> I believe that when you repeatedly `push!` , extra space is allocated, something like doubling (or 1.5x?) the buffer each time you overflow the old.

Yes, doubling:

> <https://github.com/JuliaLang/julia/blob/master/src/array.c#L839>

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 24, 2019, 10:45pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/7 "2019-01-24T22:45:46Z")

</div>

> [@affans](#):
>
> Is this an efficient operation? Am I correct in that by pushing to a vector, the compiler has to allocate new space, and copy the old vector over?  
> I am sure for small vectors this is a non issue, but what happens when your vector is expected to grow large? Would it allocate/copy everytime?

If you `push!` into a vector, julia over-allocates memory. Until your vector grows close to your total amount of memory, you get amortized O(1) `push!`.

Is this efficient? Well, there is an issue about `push!` being “horribly slow”, because the compiler cannot properly inline it. On my 2ghz laptop:

```julia
julia> using BenchmarkTools

julia> function pushtest(v, N)
       resize!(v,0)
       for i=1:N
       push!(v, i)
       end
       nothing
       end

julia> v=Int[];
julia> t=@belapsed pushtest(v, 10^4);
julia> t*2e9/1e4
13.1304
julia> @noinline function pushtest2(N)
       v=Int[]
       for i=1:N
       push!(v, i)
       end
       v
       end
julia> t=@belapsed pushtest2(10^4);
julia> t*2e9/1e4
15.9796
julia> t=@belapsed collect(1:10^4);
julia> t*2e9/1e4
1.5772

```

You see that `push!` is medium-fast: factor of 10 slower than memcopy, and don’t worry about the internal copying on growth.

The issue is overhead for the call into the runtime. But 15 cycles/push is still OK for most applications.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 25, 2019, 7:44am UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-variable-length-in-julia/20060/8 "2019-01-25T07:44:05Z")

</div>

> [@affans](#):
>
> Is this an efficient operation?

The right question in this context is whether the operation is efficient _conditional on_ having an unknown number of values. As others have explained, the preallocation of extra space is fairly efficient in general.

That said, if you know something about the distribution of the length, you can surely do better. Eg if you know it is \le 100 with probability 0.9999, you can use

```julia
sizehint!(v, 100)

```

at the beginning. In practice, these optimizations are rarely worth the complication.
