# Subindexing array, alternative to \`getfield.(A,:2)\`?

**URL:** <https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 23, 2017, 2:41pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651 "2017-01-23T14:41:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erihe251](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erihe251/32/503_2.png) [@erihe251](https://discourse.julialang.org/u/erihe251)\
**Post date:** [January 23, 2017, 2:41pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/1 "2017-01-23T14:41:26Z")

</div>

I want to get an array containing e.g. the second field from all objects in array `A`.

So far I’ve found:

```julia
getfield.(A,:2)

```

or

```julia
[a[2] for a in A ]

```

Is there a better or more “Julian” way? I was hoping for `A.[2]`.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 23, 2017, 3:20pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/2 "2017-01-23T15:20:40Z")

</div>

You can do `getfield.(A, [2])` too, but isn’t the comprehension more readable?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 23, 2017, 3:28pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/3 "2017-01-23T15:28:24Z")

</div>

You don’t need `:2` or `[2]`, `2` should be enough.

Also, `a[2]` will only work for tuples.

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [January 23, 2017, 3:28pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/4 "2017-01-23T15:28:47Z")

</div>

I’m confused as you ask about subindexing, but are using `getfield(A, :2)` instead of `getindex(A, :2)`.  
Do you have an array of composite types?

I had an issue with subindexing and tried `A[:][2]` - This doesn’t get the second element of each element in A, but just the second element of A.  
Edit: This is of course expected, as indexing an array with a range returns an array - in this case the whole array. Still, i would like to have some easy syntax for subindexing.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 23, 2017, 4:48pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/5 "2017-01-23T16:48:32Z")

</div>

It sounds like you want `getindex.(A, 2)` if it is an array of arrays or tuples?

There’s been some discussion of implementing `A.[2]` syntax ([https://github.com/JuliaLang/julia/issues/19169](https://github.com/JuliaLang/julia/issues/19169)), but nothing has been resolved at this point, and dot calls with `getindex` work fine.

If you have an array of composite types, `getfield.(A, 2)` should work, but will be slower than a comprehension since the compiler can’t really optimize `getfield` calls, last I checked. (In principle, we could implement optimizations for something like `getfield.(A, Val{2}())` so that the value `2` is known by the compiler, but this doesn’t seem to have been done.)

---

<div class="post-metadata">

**Author:** ![erihe251](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erihe251/32/503_2.png) [@erihe251](https://discourse.julialang.org/u/erihe251)\
**Post date:** [January 23, 2017, 5:21pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/6 "2017-01-23T17:21:45Z")

</div>

Sorry for the confusion, it’s an array of tuples in this case. I used subindexing for lack of a better word. I tried playing around with composite types as well, that’s were I went with `getfield` instead of `getindex`.

@dpsanders I agree that the comprehension is more readable. Although having a descriptive function name as a visual anchor also appeals to me.

@stevengj Thanks! I tried searching for such an issue to read up on but didn’t know the terminology.

Thanks for the answers!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 23, 2017, 7:17pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/7 "2017-01-23T19:17:43Z")

</div>

> [@erihe251](#):
>
> I agree that the comprehension is more readable

Note, however, that the `getindex.(a, 2)` version could be faster because it can fuse with other dot calls.

---

<div class="post-metadata">

**Author:** ![erihe251](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erihe251/32/503_2.png) [@erihe251](https://discourse.julialang.org/u/erihe251)\
**Post date:** [January 24, 2017, 12:38pm UTC](https://discourse.julialang.org/t/subindexing-array-alternative-to-getfield-a-2/1651/8 "2017-01-24T12:38:17Z")

</div>

Thanks for pointing that out. For anyone interested there’s a timely blog entry on [loop fusion in Julia](http://julialang.org/blog/2017/01/moredots).
