# Append! on arrays of arryas

**URL:** <https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 25, 2023, 1:19pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183 "2023-02-25T13:19:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasparalfaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasparalfaro/32/32802_2.png) [@gasparalfaro](https://discourse.julialang.org/u/gasparalfaro)\
**Post date:** [February 25, 2023, 1:19pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/1 "2023-02-25T13:19:21Z")

</div>

I want to create an array of empty arrays so I can append and have different sized arrays like this:  
 ![append_on_brackets](https://global.discourse-cdn.com/julialang/original/3X/4/2/42682aaa97b2f75ac72ae676e2d6d416c9d66f3a.jpeg)

But, if I use fill to create the array of empty arrays and then I append it the result is this:

 ![append_on_fill](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6f76f523c6b48da1de77e68494df86eb00dd666.jpeg)

Why is it any different when initializing the array of arrys with fill? Does it have to do with how it saves to memory?

I found out how to initialize my array of a given number of arryays like this:

> ```julia
> N=10
> a=[]
> for n in 1:N
> push!(a,[])
> end
> 
> ```

Doing it like this allows me to append it the way I want.

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [February 25, 2023, 1:25pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/2 "2023-02-25T13:25:44Z")

</div>

In the `fill` example, you are creating a single array pointing to some memory on the heap. This is passed into the `fill` function and sets every element pointing at that same array.

To get what you want, you have to create a new array for each element, e.g. with a list comphrension:

```julia
arr=[[] for _ in 1:10]

```

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [February 25, 2023, 1:42pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/3 "2023-02-25T13:42:50Z")

</div>

The Julia documentation for the `fill()` covers this explicitly.  
`?fill` in the Julia REPL will show you this. It is also available  
in the [online Julia documentation](https://docs.julialang.org).

---

<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:** [February 25, 2023, 1:52pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/4 "2023-02-25T13:52:32Z")

</div>

If you don’t want to stick to a predefined size, you could use a schema like this …

```julia
julia> arr2=Vector{Int64}[]
Vector{Int64}[]

julia> push!(arr2,[])
1-element Vector{Vector{Int64}}:
 []

julia> push!(arr2,[])
2-element Vector{Vector{Int64}}:
 []
 []

julia> push!(arr2[2],33)
1-element Vector{Int64}:
 33

julia> push!(arr2[1],22)
1-element Vector{Int64}:
 22

julia> arr2
2-element Vector{Vector{Int64}}:
 [22]
 [33]

...

```

PS  
I am not responsible for any adverse side effects

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 25, 2023, 2:45pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/5 "2023-02-25T14:45:34Z")

</div>

Btw this is going to have some pretty bad performances depending on what you actually want to do

---

<div class="post-metadata">

**Author:** ![gasparalfaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasparalfaro/32/32802_2.png) [@gasparalfaro](https://discourse.julialang.org/u/gasparalfaro)\
**Post date:** [February 25, 2023, 3:15pm UTC](https://discourse.julialang.org/t/append-on-arrays-of-arryas/95183/6 "2023-02-25T15:15:15Z")

</div>

Ah, thank you!
