# 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:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [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/1 "2020-10-15T17:44:01Z")

</div>

Suppose if I had the following array:

```julia
even_numbers = Int32[2,4,6,8,10] # Creating a 5 element array.

```

and I made a [shallow copy](https://docs.julialang.org/en/v1/base/base/#Base.copy):

```julia
b = copy(even_numbers)

```

If I were to modify or delete an element from `b` why doesn’t it reflect on `even_numbers`?

```julia
deleteat!(b, 2) # the element of 4 should be removed.

```

If I were to display `even_numbers`, `4` still exists.

```julia
5-element Array{Int32,1}:
  2
  4
  6
  8
 10

```

I always thought if you modify a shallow copy of a collection it will also modify the original collection. If you didn’t want to modify the original, you can use `deepcopy()`

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [October 15, 2020, 5:58pm UTC](https://discourse.julialang.org/t/why-doesnt-modifying-a-shallow-copy-of-an-array-reflect-on-the-original/48445/2 "2020-10-15T17:58:49Z")

</div>

Because even a shallow copy has to mean something other than just adding a reference to the same object. The behavior you expect you would get from doing

```julia
b = even_numbers

```

This is a better example of a shallow copy:

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

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

julia> push!(b[1], 5)
3-element Array{Int64,1}:
 1
 2
 5

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

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

```

---

<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]

```
