# How to distinguish between "found" and "not found" in \`findfirst(dict)\`? E.g.:\`\`\`

**URL:** <https://discourse.julialang.org/t/how-to-distinguish-between-found-and-not-found-in-findfirst-dict-e-g/56014>\
**Category:** General Usage\
**Created:** [February 25, 2021, 1:04pm UTC](https://discourse.julialang.org/t/how-to-distinguish-between-found-and-not-found-in-findfirst-dict-e-g/56014 "2021-02-25T13:04:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 25, 2021, 1:04pm UTC](https://discourse.julialang.org/t/how-to-distinguish-between-found-and-not-found-in-findfirst-dict-e-g/56014/1 "2021-02-25T13:04:57Z")

</div>

How to distinguish between “found” and “not found” in `findfirst(dict)`? E.g.:

```julia
d = Dict(nothing =&gt; 1, 2 =&gt; 3)
findfirst(==(3), d) # == 2, ok: d[2] == 3
findfirst(==(1), d) # == nothing, ok: d[nothing] = 1
findfirst(==(2), d) # == nothing, but actually not found

```

For now I do an extra check that `f(d[findfirst(f, d)]) == true`, but this doesn’t look clean…

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614258275345200?thread_ts=1614258275.345200&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [February 25, 2021, 9:29pm UTC](https://discourse.julialang.org/t/how-to-distinguish-between-found-and-not-found-in-findfirst-dict-e-g/56014/2 "2021-02-25T21:29:27Z")

</div>

I guess you could write your own `findfirst` method that allows you to specify the “not found” return value.

Maybe something like:

```julia
function myfindfirst(pred::Function, A, notfound=nothing)
    for (k, v) in pairs(A)
        pred(v) && return k
    end
    return notfound
end

```

```julia
d = Dict(nothing=>1, 2=>3)
myfindfirst(==(3), d, :notfound) # 2
myfindfirst(==(1), d, :notfound) # nothing
myfindfirst(==(2), d, :notfound) # :notfound

```
