# An array of empty arrays

**URL:** <https://discourse.julialang.org/t/an-array-of-empty-arrays/5476>\
**Category:** New to Julia\
**Created:** [August 20, 2017, 6:49pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476 "2017-08-20T18:49:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 20, 2017, 6:49pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/1 "2017-08-20T18:49:34Z")

</div>

Is this the best, or only, way to do this ?

```julia
julia> a=Array{Any,1}(5)
5-element Array{Any,1}:
 #undef
 #undef
 #undef
 #undef
 #undef

julia> fill!(a,[])
5-element Array{Any,1}:
 Any[]
 Any[]
 Any[]
 Any[]
 Any[]

```

just curious. I would have done this in python with

[]\*5

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 20, 2017, 7:25pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/2 "2017-08-20T19:25:36Z")

</div>

```julia
julia> [[] for i=1:5]
5-element Array{Array{Any,1},1}:
 Any[]
 Any[]
 Any[]
 Any[]
 Any[]

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 20, 2017, 7:30pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/3 "2017-08-20T19:30:54Z")

</div>

What you have is identical to

```julia
julia> fill([], 5)
5-element Array{Array{Any,1},1}:
 Any[]
 Any[]
 Any[]
 Any[]
 Any[]

```

Note that the arrays you filled in are the same one (i.e. same as `[[]] * 5` in python).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 20, 2017, 7:57pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/4 "2017-08-20T19:57:31Z")

</div>

As @yuyichao alluded to, there is a difference between the `fill` and comprehension techniques: do you want an array of the _same_ empty array, or an array of _different_ empty arrays? This will affect what happens if you modify one of the arrays:

```julia
julia> a = fill([], 5)
5-element Array{Array{Any,1},1}:
 Any[]
 Any[]
 Any[]
 Any[]
 Any[]

julia> push!(a[1], "hello"); a
5-element Array{Array{Any,1},1}:
 Any["hello"]
 Any["hello"]
 Any["hello"]
 Any["hello"]
 Any["hello"]

julia> b = [[] for i=1:5]
5-element Array{Array{Any,1},1}:
 Any[]
 Any[]
 Any[]
 Any[]
 Any[]

julia> push!(b[1], "hello"); b
5-element Array{Array{Any,1},1}:
 Any["hello"]
 Any[]       
 Any[]       
 Any[]       
 Any[]  

```

Note that the `[[]]*5` construct in Python is similar to `fill` here, in that it makes a list of 5 references to the _same_ empty list:

```nohighlight
In [1]: a = [[]]*5; a
Out[1]: [[], [], [], [], []]

In [2]: a[0].append('hello'); a
Out[2]: [['hello'], ['hello'], ['hello'], ['hello'], ['hello']]

```

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [August 21, 2017, 4:28am UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/5 "2017-08-21T04:28:39Z")

</div>

oh my.

I’d forgotten about that distinction even though I had been burned by it in Python.

Yes , I most definitely wanted 5 _distinct_ arrays.

Thank you!

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [June 7, 2019, 2:10pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/6 "2019-06-07T14:10:05Z")

</div>

How can I create an empty 0-by-N array?

Let’s say N = 2, then

```julia
julia> A = Int64[[] [] ]
0×2 Array{Int64,2}

julia> vcat(A,[1 2])
1×2 Array{Int64,2}:
 1 2

```

How do I generalize this code snippet for arbitrary N?

Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 7, 2019, 2:17pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/7 "2019-06-07T14:17:01Z")

</div>

> [@timueh](#):
>
> How can I create an empty 0-by-N array?

`Array{Int}(undef, 0, N)`

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [June 7, 2019, 6:12pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/8 "2019-06-07T18:12:22Z")

</div>

perfect. I tried `Array{Int}(undef, 1, N)` the whole time. You would think I would’ve tried replacing the `1` by a `0`… 😄 … thanks anyway

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 7, 2019, 6:48pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/9 "2019-06-07T18:48:39Z")

</div>

What is the use case for this? Probably there is a better solution.

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [June 8, 2019, 8:50am UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/10 "2019-06-08T08:50:59Z")

</div>

The use case is to construct array of multi-indices. Starting from an empty array I gradually want to build up a matrix in which every row represents a multi-index. So, the output of the algorithm is something like this:

```julia
julia> A = [1 0 1; 2 0 0; 0 0 3]
3×3 Array{Int64,2}:
 1 0 1
 2 0 0
 0 0 3

```

where every row of `A` is a multi-index.

The algorithm to construct `A` is fairly simple:

1. Define an empty row vector A.
2. For every candidate multi-index s \in \mathbb{R}^{1 \times N} do:
  - If s satisfies some criterion, append A.

3. Return A.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 8, 2019, 3:11pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/11 "2019-06-08T15:11:08Z")

</div>

You probably want to build this as a Vector of Vectors, and, if necessary, convert to a matrix only at the end.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 8, 2019, 3:53pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/12 "2019-06-08T15:53:21Z")

</div>

> [@dpsanders](#):
>
> You probably want to build this as a Vector of Vectors

Or a `Vector` of `SVector` (from StaticArrays.jl), if you want to take advantage of compile-time optimizations for the length of your multi-index, not to mention more efficient inline storage in the array. A `Vector` of `SVector` can also be converted to a `Matrix` at the end with a simple `reinterpret` (no copies) if needed.

---

<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:** [June 8, 2019, 4:17pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/13 "2019-06-08T16:17:11Z")

</div>

> [@timueh](#):
>
> The use case is to construct array of multi-indices.

Isn’t this what `CartesianIndices` are for?

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [June 8, 2019, 6:46pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/14 "2019-06-08T18:46:50Z")

</div>

thanks for the input!

---

<div class="post-metadata">

**Author:** ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)\
**Post date:** [June 8, 2019, 6:49pm UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/15 "2019-06-08T18:49:54Z")

</div>

thanks for the pointer. I don’t see the advantage immediately, but I guess [reading this](https://julialang.org/blog/2016/02/iteration) should help.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 13, 2019, 4:00am UTC](https://discourse.julialang.org/t/an-array-of-empty-arrays/5476/16 "2019-06-13T04:00:34Z")

</div>

> [@timueh](#):
>
> I don’t see the advantage immediately

An array of an immutable container with a fixed number of entries (like a `CartesianIndex` or a `StaticArray`) is typically vastly more efficient than a generic array of arrays. An array of arrays is essentially like an array of pointers, or a `sometype**` in C/C++, whereas an array of immutables is like an array of structs that are stored inline. (And the fact that the length of these immutable containers is known to the compiler enables an array of optimization possibilities that can make a big difference for small containers).
