# Filtering list from a list

**URL:** https://discourse.julialang.org/t/filtering-list-from-a-list/88074
**Category:** General Usage
**Tags:** question
**Created:** [October 1, 2022, 7:21am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074 "2022-10-01T07:21:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)
#### Post date: [October 1, 2022, 7:21am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074/1 "2022-10-01T07:21:03Z")

</div>

I have two lists: one containing a list of tuples (numbers) and the other the list of numbers. For instance, L = [(2,3),(5,6), (4,6),…] and E = [2,5,7,3…]. Now, are there any other better ways to do similar to the following?

```julia
V = [filter(x->x[1]== e, L) for e in E]

```

Thanks in advance.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [October 1, 2022, 7:58am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074/2 "2022-10-01T07:58:36Z")

</div>

The desired result is not well defined in your example. Your version produces empty tuples or doubled entries in the result depending on values in L and E.  
Here are two variants:

```julia
filter( x -> x[1] in E, L)

```

But depending on you data and size this may be much slower as your version.  
If E can be sorted there is also:

```julia
filter( x -> insorted(x[1],E), L)

```

which will be much faster if E is sorted.

The best solution depends on what you really want as a result and the sizes of L and E.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [October 1, 2022, 8:57am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074/3 "2022-10-01T08:57:45Z")

</div>

```julia
using SplitApplyCombine

group(t->first(t) ∈ Set(E), L)[true]

#or

groupview(t->first(t) ∈ Set(E) ,L)[true]

#or

[e for e in L if first(e) ∈ Set(E)]

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 1, 2022, 10:13am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074/4 "2022-10-01T10:13:09Z")

</div>

```julia
using SplitApplyCombine

(D -> [haskey(D,e) ? D[e] : similar(L,0) for e in E])(group(first, L))

```

gives exactly same result as OP (thanks to cute `group` function @rocco_sprmnt21 mentioned).

Yeah, it looks worse than OP expression, but possibly lower time complexity (and higher space complexity).

---

<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: [October 1, 2022, 11:59am UTC](https://discourse.julialang.org/t/filtering-list-from-a-list/88074/5 "2022-10-01T11:59:26Z")

</div>

You can use `filter` directly like this. There is no need for comprehension.

```julia
L = [(2,3), (5,6), (4,6)]; 
E = [2,5,7,3];

filter(x->first(x) in E, L)
2-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (5, 6)

```
