# Copy vector?

**URL:** <https://discourse.julialang.org/t/copy-vector/20082>\
**Category:** New to Julia\
**Created:** [January 25, 2019, 11:10am UTC](https://discourse.julialang.org/t/copy-vector/20082 "2019-01-25T11:10:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 25, 2019, 11:10am UTC](https://discourse.julialang.org/t/copy-vector/20082/1 "2019-01-25T11:10:01Z")

</div>

I just realized this behavior:

```julia
julia> x = [0. , 0.]
2-element Array{Float64,1}:
 0.0
 0.0

julia> y = x
2-element Array{Float64,1}:
 0.0
 0.0

julia> y[1] = 1.
1.0

julia> x
2-element Array{Float64,1}:
 1.0
 0.0

```

`x` and `y` are just different names for the same vector. How do I copy a vector into another one, then?

Also, is there any style suggestion to avoid running into errors given that we could be modifying previous values of array variables this way?

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 25, 2019, 11:13am UTC](https://discourse.julialang.org/t/copy-vector/20082/2 "2019-01-25T11:13:42Z")

</div>

you can (should?) use `copy`

```julia
julia> x = [0., 0.];
julia> y = copy(x);
julia> y[1] = 1.;
julia> x
2-element Array{Float64,1}:
 0.0
 0.0

```

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [January 25, 2019, 11:18am UTC](https://discourse.julialang.org/t/copy-vector/20082/3 "2019-01-25T11:18:19Z")

</div>

Try `copy` or `deepcopy`, should do it. This is an intrinsic feature of the language; it is not a problem as long as one is aware of it.

---

<div class="post-metadata">

**Author:** ![ale](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ale](https://discourse.julialang.org/u/ale)\
**Post date:** [April 7, 2019, 5:32pm UTC](https://discourse.julialang.org/t/copy-vector/20082/4 "2019-04-07T17:32:26Z")

</div>

Hi everyone,

I’m quite new to Julia and quite recently realized the same issue pointed out by @lmiq.

To sum up, as a general suggestion, would you recommend **always** using `copy` if interested in, e.g., `y = copy(x)`, such that from this line of code onward I would like `x` and `y` to be absolutely independent?

As a side note, I would ask in which cases it could be useful to use `=`.

Thanks in advance.

---

<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:** [April 7, 2019, 5:38pm UTC](https://discourse.julialang.org/t/copy-vector/20082/5 "2019-04-07T17:38:06Z")

</div>

> [@ale](#):
>
> I would like `x` and `y` to be absolutely independent?

Use `deepcopy`.

> [@ale](#):
>
> in which cases it could be useful to use `=`

`=` is for assignment in Julia.

(Also, please don’t resurrect old topics).

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 7, 2019, 6:00pm UTC](https://discourse.julialang.org/t/copy-vector/20082/6 "2019-04-07T18:00:05Z")

</div>

There are a few algorithms where one wants to “swap” two vectors.  
Instead of copying them over, one could just switch the names:

```julia
x, y = y, x

```
