# How to Find the First Instance of a Value in an Array

**URL:** <https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290>\
**Category:** New to Julia\
**Tags:** question, first-steps\
**Created:** [February 15, 2021, 1:55am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290 "2021-02-15T01:55:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 15, 2021, 1:55am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/1 "2021-02-15T01:55:44Z")

</div>

Hi all,

I was wondering why this wasn’t working:

```julia
mylist = ["a", "b", "c"]
findfirst(["a"], mylist)

```

As I expected it to work in at least Julia 1.6. Instead I got a stacktrace that looked like this:

```julia
ERROR: MethodError: no method matching findfirst(::Vector{String}, ::Vector{String})
Closest candidates are:
  findfirst(::Function, ::Union{AbstractString, AbstractArray}) at array.jl:1906
  findfirst(::Function, ::Any) at array.jl:1898
  findfirst(::AbstractArray) at array.jl:1824
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[22]:1

```

~ tcp 🌳

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [February 15, 2021, 1:58am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/2 "2021-02-15T01:58:28Z")

</div>

Instead, thanks to @mcabbott and @jling, I discovered this is the syntax I wanted:

```julia
findfirst(==("a"), mylist)

```

which returns `1` as expected. The following syntax is a bit more expressive and easier to read:

```julia
findall(x -> x == "a", mylist)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 16, 2021, 12:09am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/3 "2021-02-16T00:09:35Z")

</div>

> [@TheCedarPrince](#):
>
> `findfirst(==("a"), mylist)`

Or

```julia
findfirst(isequal("a"), mylist)

```

Is clear too.

---

<div class="post-metadata">

**Author:** ![DaymondLing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daymondling/32/8474_2.png) [@DaymondLing](https://discourse.julialang.org/u/DaymondLing)\
**Post date:** [February 16, 2021, 3:45am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/4 "2021-02-16T03:45:39Z")

</div>

`findfirst(mylist .== "a")` is quite easy to read as well.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 16, 2021, 4:58am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/5 "2021-02-16T04:58:29Z")

</div>

Its easy to read, but it creates intermediate array (i.e. allocates) and as such it is slower than other versions.

---

<div class="post-metadata">

**Author:** ![DaymondLing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daymondling/32/8474_2.png) [@DaymondLing](https://discourse.julialang.org/u/DaymondLing)\
**Post date:** [February 17, 2021, 2:41am UTC](https://discourse.julialang.org/t/how-to-find-the-first-instance-of-a-value-in-an-array/55290/6 "2021-02-17T02:41:58Z")

</div>

Agree, thank you.
