# Inconsistent behavior with double indexing?

**URL:** <https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904>\
**Category:** General Usage\
**Created:** [August 23, 2019, 5:24pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904 "2019-08-23T17:24:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cwatson/32/7667_2.png) [@cwatson](https://discourse.julialang.org/u/cwatson)\
**Post date:** [August 23, 2019, 5:24pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904/1 "2019-08-23T17:24:07Z")

</div>

Consider objects `a` and `b`, which are array of arrays defined below. These objects are equal according to Julia and the only difference is in how I create them. But double-indexing works very differently on each.

```julia
julia> a=fill([3,4],2)
2-element Array{Array{Int64,1},1}:
 [3, 4]
 [3, 4]

julia> b=[[3,4],[3,4]]
2-element Array{Array{Int64,1},1}:
 [3, 4]
 [3, 4]

julia> a==b
true

julia> a[1][1]=5
5

julia> a
2-element Array{Array{Int64,1},1}:
 [5, 4]
 [5, 4]

julia> b[1][1]=5
5

julia> b
2-element Array{Array{Int64,1},1}:
 [5, 4]
 [3, 4]

```

Can anyone explain (1) why `a` and `b` behave differently or (2) how I can modify my code to have it always behave like `b`, which seems like the intuitively correct result?

---

<div class="post-metadata">

**Author:** ![raphbacher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raphbacher/32/6019_2.png) [@raphbacher](https://discourse.julialang.org/u/raphbacher)\
**Post date:** [August 23, 2019, 5:37pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904/2 "2019-08-23T17:37:26Z")

</div>

The issue is not with indexing but at the creation of `a` : when you use `fill([3,4],2)`, both elements of `a` point to the same object in memory (here a vector of value [3,4]). You can check that `a[1]===a[2]` (note the triple = ) whereas `b[1] !=== b[2]`. Which means that modifying this object will be reflected in all its occurrences.

EDIT : You can completely change `a[1]` with `a[1]=[5,4]` (it will not mutate the old a[1] but create a new element that is not linked to “old” `a[1]` and `a[2]` (that will be preserved). But I’m not sure it is easy to do otherwise.

---

<div class="post-metadata">

**Author:** ![cwatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cwatson/32/7667_2.png) [@cwatson](https://discourse.julialang.org/u/cwatson)\
**Post date:** [August 23, 2019, 5:59pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904/3 "2019-08-23T17:59:17Z")

</div>

Thanks for explaining why this is happening.

My overall goal was to create an array with placeholder `NaN` values using `fill`, then use a `for` loop to replace each entry. This would need a `fill` function that creates copies instead of references (I’m not sure how to accomplish this but it may involve the `deepcopy` function).

Some other things that might work: adding one dimension to the array instead of using an array of arrays (this only works if the sizes are consistent), or `push!`-ing each new array to build up an array over iterations. These are probably better solutions anyway.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 23, 2019, 6:00pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904/4 "2019-08-23T18:00:40Z")

</div>

An array comprehension is often a nice way to do this: `[[3,4] for _ in 1:2]`.

---

<div class="post-metadata">

**Author:** ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)\
**Post date:** [August 23, 2019, 6:14pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-with-double-indexing/27904/5 "2019-08-23T18:14:42Z")

</div>

See also the somehow related old discussion here …

> [@Create initialized arrays of structs](https://discourse.julialang.org/t/create-initialized-arrays-of-structs/7394):
>
> Dearests, today I found a disturbing (yet documented … ) behavior of julia. Let’s see: struct T a::Vector{Int}; end T(N) = T(zeros(Int,N)) Now aiming at a Vector of initialized Ts I (mistakenly) did a julia\> x=fill(T(3),2) 2-element Array{T,1}: T([0, 0, 0]) T([0, 0, 0]) The problem is that now the 2 elements in the vector are indeed the same: julia\> x[1].a[1] = 1; julia\> x 2-element Array{T,1}: T([1, 0, 0]) T([1, 0, 0]) No big deal, a comprehension would have produced my intended resu…
