# Better way to do \`\`\`getindex.\`\`\`

**URL:** https://discourse.julialang.org/t/better-way-to-do-getindex/128814
**Category:** General Usage
**Tags:** question
**Created:** [May 7, 2025, 5:45pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814 "2025-05-07T17:45:57Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [May 7, 2025, 5:45pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/1 "2025-05-07T17:45:57Z")

</div>

I often have a need to index a list of lists at a single index and use, e.g.:

`getindex.( list_of_lists, 1 )`

I would love to instead write `list_of_lists.[1]` but this seems to be invalid syntax. Is there some other way of doing `getindex.`?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 7, 2025, 5:48pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/2 "2025-05-07T17:48:54Z")

</div>

You can do `first.(x)`, but I guess that’s similar to the `getindex` solution. Using `.[1]` won’t work.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [May 7, 2025, 6:01pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/3 "2025-05-07T18:01:00Z")

</div>

In the future: `nth` [https://github.com/JuliaLang/julia/pull/56580](https://github.com/JuliaLang/julia/pull/56580)

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [May 7, 2025, 6:30pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/4 "2025-05-07T18:30:27Z")

</div>

I think it would make total sense for `A.[B]` to lower to `getindex.(A, B)`, but this proposal has proven a bit controversial in the past

---

<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: [May 7, 2025, 6:40pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/5 "2025-05-07T18:40:37Z")

</div>

> [@adienes](#):
>
> I think it would make total sense for `A.[B]` to lower to `getindex.(A, B)`, but this proposal has proven a bit controversial in the past

In particular, see [Broadcast Array Indexing · Issue #19169 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/19169) and [What to do about nonscalar indexing? · Issue #30845 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/30845)

---

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [May 7, 2025, 8:08pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/6 "2025-05-07T20:08:26Z")

</div>

Thank you. I now see that this doesn’t make sense. The “.” operator maps across the arguments which are on the right and not the function which is on the left. q, an APL-derived language used in much of finance, has two different operators for mapping leftwards and rightwards (/ and \ if I rememeber correctly). I guess something like that would be too much to hope for.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [May 7, 2025, 8:25pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/7 "2025-05-07T20:25:19Z")

</div>

Without defining new syntax, there are many hacks you can try out. I don’t promise these won’t break things… probably a bad idea except for messing around! The last of these is what I wish for most often.

```julia
julia> (x::Array)(i...) = x[i...] # make arrays callable?

julia> [[1,2,3], [3,5,6]].(2) # broadcast that call
3-element Vector{Int64}:
 3
 5
 6

julia> [[1,2,3], [3,5,6]].([1 1 2])
1×3 Matrix{Vector{Int64}}:
 [1, 2, 3] [1, 2, 3] [3, 5, 6]

julia> Base.:(~)(x::Array, i::Int) = x[i] # infix operator, but only has 1-arg methods in Base

julia> [[1,2,3], [3,5,6]].~2
2-element Vector{Int64}:
 2
 5

julia> Base.getproperty(x::Array, i::Integer) = getindex.(x, i)

julia> [[1,2,3], [3,5,6]].:2
2-element Vector{Int64}:
 2
 5

julia> [(1,2), (3,4)].:1
2-element Vector{Int64}:
 1
 3

julia> function Base.getproperty(x::Array, s::Symbol)
         s === :ref && return getfield(x, :ref)
         s === :size && return getfield(x, :ref)
         getproperty.(x, s)
       end

julia> [(a=1,b=2), (a=3,b=4)].a
2-element Vector{Int64}:
 1
 3

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [May 7, 2025, 9:51pm UTC](https://discourse.julialang.org/t/better-way-to-do-getindex/128814/8 "2025-05-07T21:51:17Z")

</div>

Both `A.:2` and `A.a` work for StructArrays with this exact semantic 🙂 They are only applicable when all elements have the same static length though.
