# Difference between deepcopy() and deepcopy.() ? Do I have to broadcast deepcopy over lists?

**URL:** <https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460>\
**Category:** General Usage\
**Tags:** question, deepcopy\
**Created:** [February 11, 2020, 10:02am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460 "2020-02-11T10:02:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [February 11, 2020, 10:02am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460/1 "2020-02-11T10:02:13Z")

</div>

I have seen a behaviour that confused me a little bit. I have a list with structs (list\_of\_structs) and I selected specific indices of it with an integer array (idx\_array). When I then manipulate structs of the list\_of\_structs, which had the same index of the idx\_array, I manipulate all of them. After that I used deepcopy, but this led to the same behaviour. Then I used the broadcast version of deepcopy, which works fine, but now I´m not sure when I have to use deepcopy() and when deepcopy.().

```julia
list_of_structs = deepcopy(list_of_strucs[idx_array])

```

```julia
list_of_structs = deepcopy.(list_of_strucs[idx_array])

```

In example I have a struct with multiple different data types in it, including a list of structs as in the example above. Do I have to use deepcopy() or deepcopy.() to make independent copy of my\_struct1 ?

```julia
mutable struct MyStruct2
  var1::Array{Int64}
  var2::Float64
end

mutable struct MyStruct1
   list_of_structs::Array{MyStruct2}
   var1::Int64
   var2::DataFrame
end
```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 11, 2020, 10:24am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460/2 "2020-02-11T10:24:35Z")

</div>

If you want independent copies of vectors of `MyStruct1`s, which themselves have arrays of `MyStruct2`, which contains arrays, you should make deep copies.

I am assuming that the “list” here is a `Vector`. `deepcopy` should be fine since it will copy the vector itself deeply.

(incidentally, you may want to [avoid abstract type parameters](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-containers-with-abstract-type-parameters-1))

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [February 11, 2020, 10:35am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460/3 "2020-02-11T10:35:41Z")

</div>

> [@Tamas\_Papp](#):
>
> [avoid abstract type parameters](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-containers-with-abstract-type-parameters-1)

Ok I have indeed an abstract type of Array{Array{Number}} in MyStruct2. So it would be better to store it in an Array{Array{Float64}} data type and then to cast it when I need it to be an Int64?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 11, 2020, 10:55am UTC](https://discourse.julialang.org/t/difference-between-deepcopy-and-deepcopy-do-i-have-to-broadcast-deepcopy-over-lists/34460/4 "2020-02-11T10:55:49Z")

</div>

That depends on the context (which I don’t have), a type parameter may be enough.
