# Get the Vector of a Vector of struct

**URL:** <https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000>\
**Category:** New to Julia\
**Created:** [February 8, 2022, 8:54am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000 "2022-02-08T08:54:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [February 8, 2022, 8:54am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/1 "2022-02-08T08:54:43Z")

</div>

I have a vector of a struct and want a vector of a component of the struct:

```julia
struct MyPair
  x::Float64
  y::Float64
end

vec = Vector{MyPair}()
push!(vec, MyPair(1,10))
push!(vec, MyPair(2,20))
push!(vec, MyPair(3,30))

println(vec[:].x) # Doesn't compile.
println(vec.x[:]) # Doesn't compile, either.

```

Does a functionality to do this already exist in Julia or in a Julia package?

I can easily write a function to extract the vector of the component but it’s a bit tedious to have to write such a function for each struct I define . . .

(Before introducing the struct, I was using a 2D array like arr[1,:] for vec.x and arr[2,:] for vec.y but I always get confused as to whether “1” represents “x” or not . . . The struct introduces an annotation, if you like.)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 8, 2022, 9:13am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/2 "2022-02-08T09:13:29Z")

</div>

Are you looking for:

```julia
julia> getproperty.(vec, :x)
3-element Vector{Float64}:
 1.0
 2.0
 3.0

```

?

---

<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:** [February 8, 2022, 10:08am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/3 "2022-02-08T10:08:15Z")

</div>

[StructArrays.jl](https://github.com/JuliaArrays/StructArrays.jl) allows you to have an array which gives structs on iteration whilst internally storing individual fields in separate arrays.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 8, 2022, 1:33pm UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/4 "2022-02-08T13:33:10Z")

</div>

You could also spit out the vector with a comprehension:

```julia
[v.x for v in vec0]

```

_ **NB:** _ _renamed_ `vec` _to_ `vec0` _to not collide with the Base function with the same name_

---

<div class="post-metadata">

**Author:** ![FrancisKing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisking/32/24966_2.png) [@FrancisKing](https://discourse.julialang.org/u/FrancisKing)\
**Post date:** [February 8, 2022, 5:37pm UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/5 "2022-02-08T17:37:00Z")

</div>

Or, for completeness:

```
 map (v -> getproperty(v,:x),vec)

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [February 9, 2022, 8:15am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/6 "2022-02-09T08:15:26Z")

</div>

Thank you all for your inputs. Each of your solutions, of course, works for my problem.

For completeness 🙂 I have a related question.

I also have a vector of vectors. It’s of course very similar to a 2D array, but I use a vector of vectors because both the child and parent vectors individually grow as I read data from a file. The following code omits the reading-from-file part just for simplicity:

```julia
vv = Vector{Vector{Int}}()
push!(vv, [1, 2, 3])
push!(vv, [10, 20, 30])
println(vv[:][1]) # I want the first "column" [1, 10] but you actually get [1,2,3].
a = [1 2 3; 10 20 30]
println(a[:, 1]) # we get [1,10]

```

So, what should we do to get the first “column” of a vector of vectors?

In my original question of this thread, we have `getproperty` such that

```julia
getproperty(s, :x) # is equivalent to s.x

```

So, I imagine there may be a `getelement` function such that

```julia
getelement(v, 1) # equivalent to v[1]

```

If there is such a function, we would be able to get the first “column” of the vector of vectors as

```julia
getelement.(vv, 1)

```

wouldn’t we?

Currently, I use the list comprehension `[v[1] for v in vv]`, which is arguably easier to understand than the above. (That’s why I said “for completeness” . . . )

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 9, 2022, 8:29am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/7 "2022-02-09T08:29:35Z")

</div>

Broadcast `getindex()`, for example, to get the second element do: `getindex.(vv, 2)`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 9, 2022, 8:32am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/8 "2022-02-09T08:32:14Z")

</div>

Or in the special case of getting the first element from each, `first.(vv)`

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [February 9, 2022, 8:47am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/9 "2022-02-09T08:47:29Z")

</div>

Thanks!!! But why get **index**? Although I saw the name somewhere, it’s never occurred to me that it actually gets an element from a vector . . .

Perhaps is it short for “ **get** value from vv at **index** i” ? But if that was the intention, I would call it “get\_at\_index” at the very least . . .

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 9, 2022, 9:08am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/10 "2022-02-09T09:08:27Z")

</div>

> [@ryofurue](#):
>
> But why get **index**?

Because you asked me, the answer is “I have no clue”

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 9, 2022, 9:49am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/11 "2022-02-09T09:49:29Z")

</div>

See the discussion here:

[https://github.com/JuliaLang/julia/issues/42525](https://github.com/JuliaLang/julia/issues/42525)

I guess the direct answer to your question is that name was just chosen in the early days of Julia and no one ever considered it “bad” enough to warrant a massively breaking change to the language later on. As you see in the discussion, there is a possibility that it might be renamed in 2.0.

If it’s about discoverability (i.e. you would have never guessed that `[]` is syntactic sugar for `getindex`), you can do:

```julia
julia> x = rand(5);

julia> Meta.@lower x[1]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Base.getindex(x, 1)
└── return %1
))))

```

to see what’s going on.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [February 9, 2022, 10:19am UTC](https://discourse.julialang.org/t/get-the-vector-of-a-vector-of-struct/76000/12 "2022-02-09T10:19:34Z")

</div>

> See the discussion here

Wow, thanks! That discussion already includes everything I said: `getindex` being counterintuitive, `get element`, `get at index`, etc.

Believe me, I searched Google for the function name for `[]` but failed to find `getindex`, which was the reason why I posted my question here.

> you would have never guessed that is syntactic sugar for getindex

Well, before posting my question, actually I tried

```julia
([]).(vv, 1)

```

because I guessed the notation `[]` may be a syntactic sugar for a function call.

This is similar to how C++ and Haskell define syntactic sugars.

> `Meta.@lower`

Reflection capability of Julia is amazing to me. Thanks for that.
