Home Explore Blog CI



nushell

commands/docs/filter.md
95a18933c8169067a451d5c14ae35762b7eeccaf4c8bc36e000000030000072d
---
title: filter
categories: |
  filters
version: 0.104.0
filters: |
  Filter values based on a predicate closure.
usage: |
  Filter values based on a predicate closure.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `filter` for [filters](/commands/categories/filters.md)

<div class='command-title'>Filter values based on a predicate closure.</div>

## Signature

```> filter {flags} (closure)```

## Parameters

 -  `closure`: Predicate closure.


## Input/output types:

| input     | output    |
| --------- | --------- |
| list\<any\> | list\<any\> |
| range     | list\<any\> |
## Examples

Filter items of a list according to a condition
```nu
> [1 2] | filter {|x| $x > 1}
╭───┬───╮
│ 0 │ 2 │
╰───┴───╯

```

Filter rows of a table according to a condition
```nu
> [{a: 1} {a: 2}] | filter {|x| $x.a > 1}
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 2 │
╰───┴───╯

```

Filter rows of a table according to a stored condition
```nu
> let cond = {|x| $x.a > 1}; [{a: 1} {a: 2}] | filter $cond
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 2 │
╰───┴───╯

```

Filter items of a range according to a condition
```nu
> 9..13 | filter {|el| $el mod 2 != 0}
╭───┬────╮
│ 0 │  9 │
│ 1 │ 11 │
│ 2 │ 13 │
╰───┴────╯

```

List all numbers above 3, using an existing closure condition
```nu
> let a = {$in > 3}; [1, 2, 5, 6] | filter $a

```

## Notes
This command works similar to 'where' but allows reading the predicate closure from
a variable. On the other hand, the "row condition" syntax is not supported.

Chunks
14149264 (1st chunk of `commands/docs/filter.md`)
Title: filter - Filter values based on a predicate closure
Summary
The `filter` command in Nushell allows filtering values from a list or table based on a predicate closure. It takes a closure as input and returns a new list containing only the elements that satisfy the condition defined in the closure. The examples demonstrate filtering lists, tables, and ranges using different conditions.