# Syntax for extracting keys from vector of pairs?

**URL:** https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063
**Category:** General Usage
**Tags:** arrays
**Created:** [June 20, 2025, 2:27pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063 "2025-06-20T14:27:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [June 20, 2025, 2:27pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063/1 "2025-06-20T14:27:46Z")

</div>

```julia
julia> X
3-element Vector{Pair{String, Any}}:
  "water" => [1 2]
 "coffee" => [3 4 5]
    "tea" => [6 7 8 9]

```

To extract the keys from X, the current syntax is first.(X). However, given that the syntax for extracting a column from a single pair uses indexing like this

```julia
julia> X[2][1]
"coffee"

julia> X[2][2]
1×3 Matrix{Int64}:
 3 4 5

```

wouldn’t a more “regular form” of the syntax to extract all keys be X.[1] instead of first.(X)?

---

<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: [June 20, 2025, 2:38pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063/2 "2025-06-20T14:38:58Z")

</div>

See this discussion and issues linked therein:

> <https://github.com/JuliaLang/julia/issues/19169>
>
> I have an array \`x::Array{ImmutableArray.Vector2}\`.
> 
> It would be cool to be ab…le to broadcast indexing as is done with functions.
> e.g.
> \`\`\`x.\[1\]\`\`\`,
> which would return the first element of each \`Vector2\` in my array.
> 
> This is currently achievable with \`getindex.(x,1)\` or in the case of a range of indices
> \`getindex.(x,\[range\])\`.

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [June 20, 2025, 5:02pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063/3 "2025-06-20T17:02:55Z")

</div>

You can use a general index with `getindex`.

```julia
julia> getindex.(X, 1)
3-element Vector{String}:
 "water"
 "coffee"
 "tea"

julia> getindex.(X, 2)
3-element Vector{Matrix{Int64}}:
 [1 2]
 [3 4 5]
 [6 7 8 9]

```

If you use a dictionary instead of vector of pairs, you can get the keys with the `keys` function.

```julia
julia> using OrderedCollections

julia> Xdict = OrderedDict(X)
OrderedDict{String, Matrix{Int64}} with 3 entries:
  "water" => [1 2]
  "coffee" => [3 4 5]
  "tea" => [6 7 8 9]

julia> keys(Xdict)
KeySet for a OrderedDict{String, Matrix{Int64}} with 3 entries. Keys:
  "water"
  "coffee"
  "tea"

julia> values(Xdict)
ValueIterator for a OrderedDict{String, Matrix{Int64}} with 3 entries. Values:
  [1 2]
  [3 4 5]
  [6 7 8 9]

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [June 20, 2025, 5:24pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063/4 "2025-06-20T17:24:05Z")

</div>

As of yesterday, Julia v1.13 has a new `Iterators.nth` that generalizes `first` and `last`. And it has a “functor” like form that you can broadcast:

```julia-repl
julia> X = [
         "water" => [1 2]
        "coffee" => [3 4 5]
           "tea" => [6 7 8 9]];

julia> Iterators.nth(1).(X)
3-element Vector{String}:
 "water"
 "coffee"
 "tea"

julia> Iterators.nth(2).(X)
3-element Vector{Matrix{Int64}}:
 [1 2]
 [3 4 5]
 [6 7 8 9]

```

Yeah, I know it’s not as “pretty” as an indexing expression. But you can now easily make ordinal numbers work (albeit with parentheses or piping; the precedence of the function call “wins” over the juxtaposition):

```Julia-repl
julia> struct OrdinalIndicator; end

julia> Base.:*(i::Integer, ::OrdinalIndicator) = Iterators.nth(i)

julia> const ᵗʰ = const ʳᵈ = const ⁿᵈ = const ˢᵗ = OrdinalIndicator()
OrdinalIndicator()

julia> (1ˢᵗ).(X)
3-element Vector{String}:
 "water"
 "coffee"
 "tea"

julia> X .|> 2ⁿᵈ
3-element Vector{Matrix{Int64}}:
 [1 2]
 [3 4 5]
 [6 7 8 9]

```

(and yes, this is mostly a joke)

---

<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: [June 20, 2025, 10:59pm UTC](https://discourse.julialang.org/t/syntax-for-extracting-keys-from-vector-of-pairs/130063/5 "2025-06-20T22:59:14Z")

</div>

> [@mbauman](#):
>
> this is mostly a joke

But a nice joke.  
Another variant of it:

```julia
_¹ˢᵗ = Iterators.nth(1)
_²ⁿᵈ = Iterators.nth(2)
_³ʳᵈ = Iterators.nth(3)
_⁴ᵗʰ = Iterators.nth(4)
_⁵ᵗʰ = Iterators.nth(5)
# ...

```
