# Why x\[\[1,2\]\] = \[0,0\] works?

**URL:** <https://discourse.julialang.org/t/why-x-1-2-0-0-works/18615>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 13, 2018, 12:07am UTC](https://discourse.julialang.org/t/why-x-1-2-0-0-works/18615 "2018-12-13T00:07:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2018, 12:07am UTC](https://discourse.julialang.org/t/why-x-1-2-0-0-works/18615/1 "2018-12-13T00:07:42Z")

</div>

Why this works:

julia\> x = collect(1:5);

julia\> x[[1,2]] = [0,0]  
2-element Array{Int64,1}:  
0  
0

julia\> x  
5-element Array{Int64,1}:  
0  
0  
3  
4  
5

I thought `x[[1,2]] = [0,0]` should be a syntax error (the correct syntax being `x[[1,2]] .= [0,0]`.

So what is going on when I type `x[[1,2]] = [0,0]`? Is it different from `x[[1,2]] .= [0,0]`?

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [December 13, 2018, 12:41am UTC](https://discourse.julialang.org/t/why-x-1-2-0-0-works/18615/2 "2018-12-13T00:41:31Z")

</div>

In an expression of the form `x[i]=y`, the left-hand side `x[i]` is _not_ evaluated first. Instead, it is interpreted as `Base.setindex!(x,y,i)`. This is not unique to Julia: Matlab and Python basically do the same.

On the other hand, `x[i].=y` becomes `broadcast!(identity,view(x,i),y)`, and more generally `x[i] .= f.(y)` is `broadcast!(f,view(x,i),y)` etc.  
This is different, since, e.g., `x[1:2] .= 1` broadcasts 1 into the index’s shape, whereas `x[1:2] = 1` is an error.  
Also, if you do `x[i] = f.(y)` you are asking for a temporary array `f.(y)`, whereas `x[i] .= f.(y)` is an in-place computation.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2018, 1:21pm UTC](https://discourse.julialang.org/t/why-x-1-2-0-0-works/18615/3 "2018-12-13T13:21:40Z")

</div>

Thanks for the detailed reply!
