# Why doesn't modifying a shallow copy of an array reflect on the original?

**URL:** <https://discourse.julialang.org/t/why-doesnt-modifying-a-shallow-copy-of-an-array-reflect-on-the-original/48445>\
**Category:** New to Julia\
**Created:** [October 15, 2020, 5:44pm UTC](https://discourse.julialang.org/t/why-doesnt-modifying-a-shallow-copy-of-an-array-reflect-on-the-original/48445 "2020-10-15T17:44:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 15, 2020, 7:43pm UTC](https://discourse.julialang.org/t/why-doesnt-modifying-a-shallow-copy-of-an-array-reflect-on-the-original/48445/3 "2020-10-15T19:43:32Z")

</div>

You may understand shallow as “just one level” and deep as “all levels”.

First let us have a vector of vectors, a shallow copy of it, and a deep copy of it.

```julia
julia> a1, a2, a3 = [1, 2, 3], [4, 5, 6], [7, 8, 9]
([1, 2, 3], [4, 5, 6], [7, 8, 9])

julia> a123 = [a1, a2, a3]
3-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]

julia> shallow = copy(a123);

julia> deep = deepcopy(a123);

```

Now, if we mutate the first element inside the original vector (`a123`), this reflects in the `shallow` but not in the `deep`.

```julia
julia> a123[1][1] = 10
10

julia> shallow[1]
3-element Array{Int64,1}:
 10
  2
  3

julia> deep[1]
3-element Array{Int64,1}:
 1
 2
 3

```

The reason is that `shallow` has allocated a new vector with the same three inner vectors, i.e., it copied just the first level that is the external vector, but did not copy the second level that is each element. `shallow` has the same elements as `a123` inside it. The `deepcopy` has created a copy of each element thus remain unaffected.

To check if the `copy` has really copied the first level (i.e., the outer vector), we can change the outer vector (instead of the elements inside it) and see the changes do not reflect in the original.

```julia
julia> shallow[1] = [30, 20, 10];

julia> shallow
3-element Array{Array{Int64,1},1}:
 [30, 20, 10]
 [4, 5, 6]
 [7, 8, 9]

julia> a123
3-element Array{Array{Int64,1},1}:
 [10, 2, 3]
 [4, 5, 6]
 [7, 8, 9]

```

---

_[View the full topic](https://discourse.julialang.org/t/why-doesnt-modifying-a-shallow-copy-of-an-array-reflect-on-the-original/48445)._
