# Destructuring assignment with arrays, like in NumPy

**URL:** <https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441>\
**Category:** New to Julia\
**Created:** [September 28, 2020, 9:51pm UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441 "2020-09-28T21:51:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwindolf](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cwindolf](https://discourse.julialang.org/u/cwindolf)\
**Post date:** [September 28, 2020, 9:51pm UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441/1 "2020-09-28T21:51:21Z")

</div>

Hi!

So, for an example, I have a 110x2 delimited text file on disk. The first column is my variable Y, and the second column is my variable X. My goal: get Y and X both in their own array, either column or row, no problem.

In Python, I can do something really nice:

```python
import numpy as np
y, x = np.loadtxt("abc.txt").T

```

In Julia, the destructuring assignment does not seem as general, so I have to do:

```julia
using DelimitedFiles
yx = readdlm("abc.txt", Float64)
y, x = yx[:, 1], yx[:, 2]

```

My question is: in Python, I can understand what destructuring does. There’s a whole PEP about it, and the idea is to call `iter(...)` on the RHS and consume the elements on the left hand side following some rules.

For Julia, the only documentation I can find about destructuring is on the [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/) page, and that deals only with tuples. But it seems like more general destructuring is possible (???).

I’m wondering, is there anything written about destructuring in Julia? How does it work? Would it be possible to emulate Python’s behavior, or reproduce the Python example above?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 28, 2020, 10:04pm UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441/2 "2020-09-28T22:04:14Z")

</div>

Any object that iterates can be destructured on assignment. You can see a draft I wrote for the docs here: [https://github.com/JuliaLang/julia/pull/30579/files](https://github.com/JuliaLang/julia/pull/30579/files) (you’ve motivated me to polish it up, any thoughts or feedback would be appreciated).

In numpy, iterating over an n-dimensional array iterates over (n-1)-dimensional arrays. In Julia, iterating over an array iterates over the elements. There is `eachcol`/`eachrow` which gives you an iterator over each column/row:

```julia
julia> A = rand(3,2)
3×2 Array{Float64,2}:
 0.607359 0.239138
 0.695322 0.581047
 0.774758 0.333456

julia> y,x = eachcol(A)
Base.Generator{Base.OneTo{Int64},Base.var"#179#180"{Array{Float64,2}}}(Base.var"#179#180"{Array{Float64,2}}([0.6073594183138431 0.23913800932018425; 0.695321894908304 0.5810467945907654; 0.7747577825874581 0.33345565551241685]), Base.OneTo(2))

julia> y
x3-element view(::Array{Float64,2}, :, 1) with eltype Float64:
 0.6073594183138431
 0.695321894908304
 0.7747577825874581

julia> x
3-element view(::Array{Float64,2}, :, 2) with eltype Float64:
 0.23913800932018425
 0.5810467945907654
 0.33345565551241685

```

Note that `x` and `y` here are views, not copies like numpy. To get that you can broadcast `copy` over the iterator:

```julia
julia> y,x = copy.(eachcol(A))
2-element Array{Array{Float64,1},1}:
 [0.6073594183138431, 0.695321894908304, 0.7747577825874581]
 [0.23913800932018425, 0.5810467945907654, 0.33345565551241685]

julia> y
x3-element Array{Float64,1}:
 0.6073594183138431
 0.695321894908304
 0.7747577825874581

julia> x
3-element Array{Float64,1}:
 0.23913800932018425
 0.5810467945907654
 0.33345565551241685

```

---

<div class="post-metadata">

**Author:** ![cwindolf](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cwindolf](https://discourse.julialang.org/u/cwindolf)\
**Post date:** [September 28, 2020, 10:06pm UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441/3 "2020-09-28T22:06:16Z")

</div>

Thank you @simonbyrne, that’s perfect.

I wish Julia could `a, b..., c = 1:10`!

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 28, 2020, 10:09pm UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441/4 "2020-09-28T22:09:24Z")

</div>

> [@cwindolf](#):
>
> I wish Julia could `a, b..., c = 1:10` !

See [https://github.com/JuliaLang/julia/issues/2626](https://github.com/JuliaLang/julia/issues/2626) for discussion on that issue. Given that it is now 7 years old, I wouldn’t hold my breath on it though (though it is still under active debate, so maybe Julia 2.0 🤞)

---

<div class="post-metadata">

**Author:** ![cwindolf](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cwindolf](https://discourse.julialang.org/u/cwindolf)\
**Post date:** [September 29, 2020, 2:09am UTC](https://discourse.julialang.org/t/destructuring-assignment-with-arrays-like-in-numpy/47441/5 "2020-09-29T02:09:53Z")

</div>

Re: the documentation you have produced, I think it’s great, and the link to the iteration interface helped me out. I hope your doc change can be merged, maybe it will help others too. (My only question is why it lives with the documentation for functions instead of, well maybe variables? but that is a small detail.)
