# Indexing (relatively) large nested list causes unintended mutation; doesn't occur after deepcopy

**URL:** <https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582>\
**Category:** General Usage\
**Tags:** question, indexing\
**Created:** [July 21, 2022, 9:01am UTC](https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582 "2022-07-21T09:01:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnabs](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@johnabs](https://discourse.julialang.org/u/johnabs)\
**Post date:** [July 21, 2022, 9:01am UTC](https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582/1 "2022-07-21T09:01:18Z")

</div>

I generated a large amount of data from a simulation I’m working on, and was extracting values from the final list of structs via map. It only behaves after making a deepcopy, as you’ll see.

Basically: My function generates 131 lists, using the following call.

```julia
final_tests=map(x->replicate_runs(30,x),50:180)

```

Each of these lists contains a set of fields, some lists of up to 30 sub-lists, each of which contains 15 elements, notably Floats64s.

In my case, I used the following function to get the last element from my simulation output:  
`ans=map(x->final_tests[x][12],1:131)`

This works as intended; however, the following code causes the list to predictably mutate:

```julia
ans[1] (prints lists of lists as expected)
ans[1] (prints first list in the previous list)
ans[1] (prints first element in previous list)
ans[1] (prints the scalar from the last one, ad infinitum)

```

When I first use b=deepcopy(ans), this behavior no longer occurs, and I can repeatedly index as much as I want. Any ideas what’s causing this? I’ll post code with permission from my PI, if this happens to be a weird bug that people have trouble reproducing.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 21, 2022, 10:52am UTC](https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582/3 "2022-07-21T10:52:52Z")

</div>

> [@johnabs](#):
>
> ```julia
> ans[1] (prints lists of lists as expected)
> ans[1] (prints first list in the previous list)
> ans[1] (prints first element in previous list)
> ans[1] (prints the scalar from the last one, ad infinitum)
> 
> ```

On each line of the above, `ans` is changing to the result of the previous line as demonstrated below.

```julia
julia> a = [[1,2,3,],[4,5,6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> ans[1]
3-element Vector{Int64}:
 1
 2
 3

julia> ans[1]
1

julia> ans[1]
1

```

This is equivalent to if you have done the following:

```julia
julia> a = [[1,2,3,],[4,5,6]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5, 6]

julia> b = a[1]
3-element Vector{Int64}:
 1
 2
 3

julia> c = b[1]
1

julia> d = c[1]
1

```

This is because `ans` is a special variable in the REPL. The value of the previously evaluated line is assigned to `ans`. Do not use `ans` as a variable name.

```julia
help?> ans
search: ans transpose transcode contains expanduser instances MathConstants

  ans

  A variable referring to the last computed value, automatically set at the
  interactive prompt.

```

---

<div class="post-metadata">

**Author:** ![johnabs](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@johnabs](https://discourse.julialang.org/u/johnabs)\
**Post date:** [July 21, 2022, 3:59pm UTC](https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582/4 "2022-07-21T15:59:12Z")

</div>

Oh, I didn’t know that was a thing! Is there a way to turn on warnings before overwriting special variables?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 21, 2022, 4:10pm UTC](https://discourse.julialang.org/t/indexing-relatively-large-nested-list-causes-unintended-mutation-doesnt-occur-after-deepcopy/84582/5 "2022-07-21T16:10:55Z")

</div>

There is an active discussion initiated by @BioTurboNick on this topic:  
[https://github.com/JuliaLang/julia/issues/43172](https://github.com/JuliaLang/julia/issues/43172)

Honestly, this “feature” is probably only intuitive to those with a MATLAB background.

I’m not sure if the currently proposed solutions cover this case, so I think this post should be linked there. If you can comment on that issue and provide a link to to post, please do so. Otherwise, let me know and I can cross reference it.

I’m also thinking we should try to rename this topic so that someone else with a similar issue can more easily find it.

Perhaps the topic could be changed to “Indexing a variable named `ans` causes apparent unintended mutation”
