# How can I copy a namedtuple or all objects within a namedtuple?

**URL:** <https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016>\
**Category:** General Usage\
**Tags:** namedtuple\
**Created:** [July 10, 2022, 10:45am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016 "2022-07-10T10:45:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonasG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasg/32/37807_2.png) [@JonasG](https://discourse.julialang.org/u/JonasG)\
**Post date:** [July 10, 2022, 10:45am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016/1 "2022-07-10T10:45:15Z")

</div>

So this should be simple (sorry if it was asked before): I have a namedtuple that saves a bunch of results. Suppose this is:

```julia
init_results = (result1 = collect(1:10), result2 = collect(11:20))

```

How can I copy init\_results?

```julia
# This assigns, but doesn't copy 
update_results = init_results 
update_results.result1[5] = 0
init_results.result1[5] # should return 5, not 0 

# Also broadcasting doesn't work
update_results .= init_results # Gives ArgumentError

```

Maybe there is something fundamentally wrong with writing code like this, in which case someone can maybe explain this to me. In my case, I have functions that save and update many different objects as results. It seems very practical to let these functions only take as input the namedtuple (instead of specifying all objects). Of course, the namedtuple is immutable, only the content within objects/entries should change.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 10, 2022, 10:56am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016/2 "2022-07-10T10:56:58Z")

</div>

The function you want is `deepcopy()`… (`copy()` works if the thing you’re copying aren’t nested containers inside containers).

I can recommend [this blog post](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/) for a more thorough discussion. The short version is, when you do `update_results = init_results` in Julia, you’re just assigning a new name to the exact same object. So when you change one, the other variable points to the same, now changed, thing.

---

<div class="post-metadata">

**Author:** ![JonasG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasg/32/37807_2.png) [@JonasG](https://discourse.julialang.org/u/JonasG)\
**Post date:** [July 10, 2022, 11:00am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016/3 "2022-07-10T11:00:03Z")

</div>

Great, I didn’t know deepcopy(). This works, thanks! Maybe you can amend your answer, since copy() doesn’t work in the example given. Then I can accept the solution.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [July 10, 2022, 11:07am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016/4 "2022-07-10T11:07:45Z")

</div>

> [@JonasG](#):
>
> This works, thanks! Maybe you can amend your answer

Done!

I should probably mention that if your code is performance critical, doing a copy on every iteration will do a lot of allocating, which can be bad for performance. If you run into this, another post with more details about what you’re doing will get you a lot of really great help from people more knowledgeable than me to speed things up. But one thing I can suggest if you don’t need to save intermediate results is to just make one copy, then update that copy as you did above, since changing elements of a vector is usually much faster than making a new vector.

---

<div class="post-metadata">

**Author:** ![JonasG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasg/32/37807_2.png) [@JonasG](https://discourse.julialang.org/u/JonasG)\
**Post date:** [July 10, 2022, 11:11am UTC](https://discourse.julialang.org/t/how-can-i-copy-a-namedtuple-or-all-objects-within-a-namedtuple/84016/5 "2022-07-10T11:11:53Z")

</div>

Thanks! This is not important in my case. This is the outer loop in my algorithm, which has very few total iterations + objects are not so big. But will keep this in mind for other problems.
