# \`filter\` a \`NamedTuple\` based on one of the fields

**URL:** <https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 20, 2019, 8:48pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122 "2019-03-20T20:48:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 20, 2019, 8:48pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/1 "2019-03-20T20:48:24Z")

</div>

How can I filter a `NamedTuple` based on a criteria applied to only one of its fields? For example

```julia
NT = (id=[1,2,3], val=[10,11,12])
NT_filtered = filter(x -> x.id == 2, NT) ## doesn't work

```

and I’m hoping for the filter to return `NT_filtered` such that `NT_filtered == (id=[2], val=[11])`.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 20, 2019, 8:52pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/2 "2019-03-20T20:52:25Z")

</div>

`x.id == [1,2,3]` and that is never going to equal `2`. Are you trying select `2` if it is in x.id, or trying to select `x.id` if it contains `2` or something else?

also fyi, if you did not know

```julia
julia> NT
(id = [1, 2, 3], val = [10, 11, 12])

julia> NT.id
3-element Array{Int64,1}:
 1
 2
 3

julia> NT[:id]
3-element Array{Int64,1}:
 1
 2
 3

julia> NT[1]
3-element Array{Int64,1}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 20, 2019, 8:58pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/4 "2019-03-20T20:58:39Z")

</div>

> [@jacob-roth](#):
>
> NT\_filtered = filter(x -\> x.id == 2, NT)

My original attempt was `NT_filtered = filter(x -> x.id .∈ [2], NT)`, but I’m missing something here, too

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [March 20, 2019, 9:01pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/5 "2019-03-20T21:01:35Z")

</div>

You could do

```julia
idx = findall(x -> x == 2, NT.id)
(id = NT.id[idx], val = NT.val[idx])

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 20, 2019, 9:06pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/6 "2019-03-20T21:06:28Z")

</div>

Thanks! I’ll use this until I figure out a way to avoid reconstructing the tuple again

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [March 20, 2019, 9:38pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/7 "2019-03-20T21:38:35Z")

</div>

As a somewhat related follow up, why doesn’t the elementwise “in” operator work here?

```julia
x = collect(1:5)
x .∈ Set([2]) ## works
x .∈ Set([2,3]) ## doesn't work

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 20, 2019, 11:44pm UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/8 "2019-03-20T23:44:54Z")

</div>

this works

```julia
x .∈ Ref(Set([2,3])) # this works

```

---

<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:** [March 21, 2019, 12:16am UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/9 "2019-03-21T00:16:01Z")

</div>

> [@jacob-roth](#):
>
> I’ll use this until I figure out a way to avoid reconstructing the tuple again

Are you worried about performance? The impact of reconstructing the tuple should be negligible compared to filtering and creating new component vectors.

If you really want to avoid reconstructing the tuple, you could _mutate_ the component vectors:

```julia
inds = findall(x -> x != 2, NT.id)
deleteat!(NT.id, inds)
deleteat!(NT.val, inds)

```

---

<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:** [March 21, 2019, 4:33am UTC](https://discourse.julialang.org/t/filter-a-namedtuple-based-on-one-of-the-fields/22122/10 "2019-03-21T04:33:53Z")

</div>

Try

```julia
using LightQuery

julia> @> NT |>
       rows |>
       when(_, @_ _.id == 2)

```
