# Variable from dictionary key

**URL:** <https://discourse.julialang.org/t/variable-from-dictionary-key/33357>\
**Category:** New to Julia\
**Created:** [January 14, 2020, 6:28pm UTC](https://discourse.julialang.org/t/variable-from-dictionary-key/33357 "2020-01-14T18:28:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PizzaQuattroStagioni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pizzaquattrostagioni/32/12308_2.png) [@PizzaQuattroStagioni](https://discourse.julialang.org/u/PizzaQuattroStagioni)\
**Post date:** [January 14, 2020, 6:28pm UTC](https://discourse.julialang.org/t/variable-from-dictionary-key/33357/1 "2020-01-14T18:28:58Z")

</div>

I have a dictionary and I want to extract a value from the dictionary and hand it over to a variable. The problem is: when I give the value from the dictionary to the variable and add a value to the variable (in the way I did it in the following example) my Dictionary changes too.  
But I don’t want to change the value of the variablekey in the dictionary.

```julia
dict=Dict{Any,Any}((0) => [1],(1) =>[2], (2) => [3],(3) => [4])
y = append!(dict[( 0 )], [1])
println("variable y is ", y)
print("dict should not change but it changed to: ", dict)

```

Thanks for help.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [January 14, 2020, 6:46pm UTC](https://discourse.julialang.org/t/variable-from-dictionary-key/33357/2 "2020-01-14T18:46:10Z")

</div>

This behavior is more due to the nature of arrays than to the fact that they are in a `Dict`. Arrays are passed by reference. If you want a distinct storage area you should `copy` the array.

---

<div class="post-metadata">

**Author:** ![PizzaQuattroStagioni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pizzaquattrostagioni/32/12308_2.png) [@PizzaQuattroStagioni](https://discourse.julialang.org/u/PizzaQuattroStagioni)\
**Post date:** [January 14, 2020, 7:37pm UTC](https://discourse.julialang.org/t/variable-from-dictionary-key/33357/3 "2020-01-14T19:37:28Z")

</div>

This is a way it works indeed:

```julia

dict=Dict{Any,Any}((0) => [1],(1) =>[2], (2) => [3],(3) => [4])
println("dict is ", dict)
y = dict[( 1 )]
println("y is ", y)
h=copy(y)
h = append!(h, [2])
println("h is ", h)
println("dict doesn't change: ", dict)

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [January 16, 2020, 6:07pm UTC](https://discourse.julialang.org/t/variable-from-dictionary-key/33357/4 "2020-01-16T18:07:50Z")

</div>

A shorter way would be using `vcat` (vertical concatenation, as one-dimensional arrays are considered “columns” in Julia):

```julia
dict=Dict{Any,Any}((0) => [1],(1) =>[2], (2) => [3],(3) => [4])
y = vcat(dict[(0)], [1])

```

The nice thing is that `vcat` can take both scalar and vector arguments, so that

```julia
y = vcat(dict[(0)], 1)

```

will just do the same.  
Also, `[a; b]` is syntactic sugar for `vcat(a, b)`, so the above can even be written as

```julia
y = [dict[(0)]; 1] # or [dict[(0)]; [1]]

```
