# Get element of a SortedDict by index instead of getting by key

**URL:** <https://discourse.julialang.org/t/get-element-of-a-sorteddict-by-index-instead-of-getting-by-key/971>\
**Category:** General Usage\
**Created:** [December 15, 2016, 4:32pm UTC](https://discourse.julialang.org/t/get-element-of-a-sorteddict-by-index-instead-of-getting-by-key/971 "2016-12-15T16:32:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 15, 2016, 4:41pm UTC](https://discourse.julialang.org/t/get-element-of-a-sorteddict-by-index-instead-of-getting-by-key/971/2 "2016-12-15T16:41:17Z")

</div>

You can always `collect()` the keys of the dict to get a vector into which you can index:

```julia
keys_as_vector = collect(keys(v))
keys_as_vector[2]

```

But this allocates a new array to hold the keys, so it may not be the most efficient approach possible.

However, if you’re always accessing the elements by index instead of by key, then perhaps a `SortedDict` isn’t really the right data structure for your case? Would a vector of `Pair`s or `Tuple`s work instead?

---

_[View the full topic](https://discourse.julialang.org/t/get-element-of-a-sorteddict-by-index-instead-of-getting-by-key/971)._
