# Getindex generator

**URL:** <https://discourse.julialang.org/t/getindex-generator/25843>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 29, 2019, 6:24pm UTC](https://discourse.julialang.org/t/getindex-generator/25843 "2019-06-29T18:24:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 29, 2019, 6:24pm UTC](https://discourse.julialang.org/t/getindex-generator/25843/1 "2019-06-29T18:24:43Z")

</div>

I can’t find it, although I’m sure it’s been answered before: how do I (efficiently) access the 'n’th element of a generator?

```julia
a = (i for i in 1:10^6)
a[3]

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 29, 2019, 7:10pm UTC](https://discourse.julialang.org/t/getindex-generator/25843/2 "2019-06-29T19:10:47Z")

</div>

```julia
first(Iterators.drop(a, n-1))

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 29, 2019, 7:13pm UTC](https://discourse.julialang.org/t/getindex-generator/25843/3 "2019-06-29T19:13:10Z")

</div>

What do you mean by efficiently? I think it is not possible in `O(1)`, the best would be `O(n)` as the Kristoffer answer.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 29, 2019, 7:25pm UTC](https://discourse.julialang.org/t/getindex-generator/25843/4 "2019-06-29T19:25:47Z")

</div>

> [@Henrique\_Becker](#):
>
> What do you mean by efficiently?

I only meant not like this:

```julia
getindex(collect(a), 3)

```
