# Reassigning value of an element in a tuple

**URL:** https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205
**Category:** General Usage
**Tags:** question
**Created:** [February 21, 2017, 6:30am UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205 "2017-02-21T06:30:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nikhil\_Anand](https://avatars.discourse-cdn.com/v4/letter/n/77aa72/32.png) [@Nikhil\_Anand](https://discourse.julialang.org/u/Nikhil_Anand)
#### Post date: [February 21, 2017, 6:30am UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205/1 "2017-02-21T06:30:33Z")

</div>

Hi, I am facing an issue with using tuples in Julia. Here’s what I am doing  
temp = [1 2 3]  
A = tuple(temp, temp)  
B = tuple(A,A,A)

B returns this :  
((  
[1 2 3],

[1 2 3]),(  
[1 2 3],

[1 2 3]),(  
[1 2 3],

[1 2 3]))

I understand I can access the elements in B by  
B[1][1][1] (which returns 1, as expected)

But if i type `B[1][1][1] = 20` in the REPL, I get this :  
((  
[20 2 3],

[20 2 3]),(  
[20 2 3],

[20 2 3]),(  
[20 2 3],

[20 2 3]))

Is there a way to change B[1][1][1] without changing the other elements ?  
Thanks in advance.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 21, 2017, 6:48am UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205/2 "2017-02-21T06:48:32Z")

</div>

Cross posted from StackOverflow. Please mention that next time.

> <https://stackoverflow.com/questions/42360192/assigning-a-value-to-an-element-of-a-tuple-in-julia>

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [February 21, 2017, 7:32am UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205/3 "2017-02-21T07:32:35Z")

</div>

Use `B=(copy(A), copy(A), copy(A))`. (BTW, no need for the `tuple`)

Also please use backticks to quote your code like so:  
```` temp = [1 2 3] A = tuple(temp, temp) B = tuple(A,A,A) ````

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [February 21, 2017, 1:46pm UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205/4 "2017-02-21T13:46:37Z")

</div>

I did found this useful:

### Equal sign (a=b)

- simple types are deep copied
- containers of simple types (or other containers) are shadow copied (their internal is only referenced, not copied)

### copy(x)

- simple types are deep copied
- containers of simple types are deep copied
- containers of containers: the content is shadow copied (the content of the content is only referenced, not copied)

### deepcopy(x)

- everything is deep copied recursively

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 21, 2017, 4:34pm UTC](https://discourse.julialang.org/t/reassigning-value-of-an-element-in-a-tuple/2205/5 "2017-02-21T16:34:16Z")

</div>

That language isn’t really correct, and it makes it sound like these functions (including `=`) have a lot more magic than they actually do. They do the same thing in every context, so there’s no need to differentiate them. As commented on SO:

> This isn’t exactly wrong… because the terminology is meaningless. There is no such thing in Julia as a “simple type”. And the functions are not magic: they do the same thing to every type. a=b sets the value of a to the value of b, copy(a) makes a new type with the same values for the fields, and deepcopy(a) does that copy recursively on each field. The only thing you need to know is that the value of an immutable is its values (fields), whereas the value of a mutable type is its pointer.
