# Replacement for find(all) on zip?

**URL:** <https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595>\
**Category:** General Usage\
**Created:** [November 16, 2018, 2:41pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595 "2018-11-16T14:41:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 16, 2018, 2:41pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/1 "2018-11-16T14:41:02Z")

</div>

In Julia 0.6, we could to

```julia
julia> find(x -> x[1] + x[2] > 0.5, zip(rand(10), rand(10)))

```

However, in Julia v1.0 this doesn’t work, because `Base.Iterators.Zip2` has no keys:

```julia
julia> findall(x -> x[1] + x[2] > 0.5, zip(rand(10), rand(10)))
ERROR: MethodError: no method matching keys(::Base.Iterators.Zip2{Array{Float64,1},Array{Float64,1}})

```

Do we have a good replacement for this?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [November 16, 2018, 8:45pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/2 "2018-11-16T20:45:57Z")

</div>

Not really AFAIK. `findall(rand(10) .+ rand(10) .> 0.5)` should do the trick, but it allocates.

See [this issue](https://github.com/JuliaLang/julia/issues/25999) for the history.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [November 16, 2018, 8:56pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/3 "2018-11-16T20:56:43Z")

</div>

```julia
filter(x -> x[1] + x[2] > 0.5, collect(zip(rand(10), rand(10))))

```

Also, this generator is much better:

```julia
(x -> x[1] + x[2] > 0.5, zip(rand(10), rand(10)))

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 16, 2018, 9:11pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/4 "2018-11-16T21:11:35Z")

</div>

Just do the find over the set of indices:

```julia
A, B = rand(10), rand(10)
findall(i->A[i]+B[i]>0.5, keys(A))

```

Actually, if you do this, you probably do want to use `filter` instead:

```julia
filter(i->A[i]+B[i]>0.5, keys(A))

```

as then you don’t need to worry about indexing back into your vector of indices.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 17, 2018, 9:43am UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/5 "2018-11-17T09:43:19Z")

</div>

Sure, that’s my current workaround. But the `collect` is expensive in my use case, it’s a lot of data.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 17, 2018, 9:48am UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/6 "2018-11-17T09:48:02Z")

</div>

> Just do the find over the set of indices:

Unfortunately, the function comes from somewhere else and is supposed to operate on values, not indices.

> Actually, if you do this, you probably do want to use `filter` instead:  
> as then you don’t need to worry about indexing back into your vector of indices.

Normally, I would, of course. But in this case, i _want_ the indices: The result is a data selector for a another large dataset (big entries). Have to avoid copies, and data may be out-of-core.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2018, 1:25pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/7 "2018-11-17T13:25:27Z")

</div>

> [@oschulz](#):
>
> Unfortunately, the function comes from somewhere else and is supposed to operate on values, not indices.

Compose it with `getindex` then.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 17, 2018, 4:39pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/8 "2018-11-17T16:39:55Z")

</div>

I’m filtering the _keys_ of the collection, not the values. Even if it’s an opaque function, this should just work. You can also customize which kinds of keys you get back (linear or Cartesian), for example:

```julia
filter(i->f(A[i], B[i]), eachindex(A,B))

```

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 17, 2018, 7:15pm UTC](https://discourse.julialang.org/t/replacement-for-find-all-on-zip/17595/9 "2018-11-17T19:15:35Z")

</div>

Yes, that might work.
