# Standard way to get first element of iterator satisfying predicate

**URL:** <https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038>\
**Category:** New to Julia\
**Created:** [June 7, 2019, 10:05am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038 "2019-06-07T10:05:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zardav](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zardav](https://discourse.julialang.org/u/zardav)\
**Post date:** [June 7, 2019, 10:05am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/1 "2019-06-07T10:05:01Z")

</div>

I have a collection and want to get the first element satisfying predicate `p`.  
One way I can achieve it is `first(x for x in col if p(x))` but I thought maybe there is more standard way, something such `first(p, col)`.  
What is the common way to do it?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [June 7, 2019, 10:19am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/2 "2019-06-07T10:19:35Z")

</div>

Are you looking for `findfirst`?

```julia
julia> x = [3,4,2,9,1,8,1,2,3,4]
10-element Array{Int64,1}:
 3
 4
 2
 9
 1
 8
 1
 2
 3
 4

julia> findfirst(x -> x > 5, x)
4

```

---

<div class="post-metadata">

**Author:** ![zardav](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zardav](https://discourse.julialang.org/u/zardav)\
**Post date:** [June 7, 2019, 10:22am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/3 "2019-06-07T10:22:58Z")

</div>

No, I want the element itself, no its index (`col` is iterable, not necessarily an array).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 7, 2019, 10:41am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/4 "2019-06-07T10:41:43Z")

</div>

You could do:

```julia
julia> x = [3,4,2,9,1,8,1,2,3,4]
10-element Array{Int64,1}:
 3
 4
 2
 9
 1
 8
 1
 2
 3
 4

julia> first(Iterators.filter(x -> x > 5, x))
9

```

---

<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:** [June 7, 2019, 10:54am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/5 "2019-06-07T10:54:12Z")

</div>

This will error if there is no element satisfying the predicate.

I immediately thought that naturally there’s a way to do this. But I can_not_ find it now.

So I made my own simple one that is fast and returns `nothing` if appropriate:

```julia
function getfirst(p, itr)
    for el in itr
        p(el) && return el
    end
    return nothing
end

```

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [June 7, 2019, 12:10pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/6 "2019-06-07T12:10:21Z")

</div>

`first(Iterators.filter(call, iterator))`

---

<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 7, 2019, 9:28pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/7 "2019-06-07T21:28:53Z")

</div>

The way you are doing it seems fine. The `if` statement you add to the generator cretes a, `filter` iterator so the suggestions with `Iterators.filter` are equivalent to what you already have.

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [June 7, 2019, 10:04pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/8 "2019-06-07T22:04:57Z")

</div>

I have literally exactly this function everywhere in my codes too 😃 (name and behavior)

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [June 8, 2019, 10:43am UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/9 "2019-06-08T10:43:06Z")

</div>

> [@DNF](#):
>
> ```julia
> function getfirst(p, itr)
> for el in itr
> p(el) && return el
> end
> return nothing
> end
> 
> ```

In case you aren’t aware, this is a good case for `Some`. It allows distinguishing found vs not found in e.g. the case `getfirst(a -> isnothing(a) || ismissing(a), ...)`.

---

<div class="post-metadata">

**Author:** ![zardav](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zardav](https://discourse.julialang.org/u/zardav)\
**Post date:** [June 9, 2019, 9:25pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/10 "2019-06-09T21:25:13Z")

</div>

Thank you all! I think `first(Iterators.filter(...))` isn’t more readable and ofcourse, it’s not shorter, so I continue to use `first` + generator. I hope that in the future there will be dedicated function for this.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [June 13, 2020, 11:16pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/11 "2020-06-13T23:16:40Z")

</div>

Why do we need to use iterators for this?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 13, 2020, 11:20pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/12 "2020-06-13T23:20:54Z")

</div>

You could use `filter` instead of `Iterators.filter`, but the first one allocates a new array, which will be very inefficient. The `Iterators` submodule provides different lazy iterators, which can be more efficient in some situations than working with arrays.

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [November 12, 2020, 12:02pm UTC](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/13 "2020-11-12T12:02:54Z")

</div>

So what is the way to get the first element of an iterator satisfying some predicate or nothing at the moment? I use

```
 iterate(x for x in l if p(x))

```

but I am still on 1.1 and there may be new ways to catch an empty iterator on1.5?
