Home Explore Blog CI



nushell

commands/docs/flatten.md
c83b7ce21c8e6e97af25855dcd8707c0d03880d52922a00100000003000009c0
---
title: flatten
categories: |
  filters
version: 0.104.0
filters: |
  Flatten the table.
usage: |
  Flatten the table.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

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

<div class='command-title'>Flatten the table.</div>

## Signature

```> flatten {flags} ...rest```

## Flags

 -  `--all, -a`: flatten inner table one level out

## Parameters

 -  `...rest`: Optionally flatten data by column.


## Input/output types:

| input     | output    |
| --------- | --------- |
| list\<any\> | list\<any\> |
| record    | table     |
## Examples

flatten a table
```nu
> [[N, u, s, h, e, l, l]] | flatten
╭───┬───╮
│ 0 │ N │
│ 1 │ u │
│ 2 │ s │
│ 3 │ h │
│ 4 │ e │
│ 5 │ l │
│ 6 │ l │
╰───┴───╯

```

flatten a table, get the first item
```nu
> [[N, u, s, h, e, l, l]] | flatten | first

```

flatten a column having a nested table
```nu
> [[origin, people]; [Ecuador, ([[name, meal]; ['Andres', 'arepa']])]] | flatten --all | get meal

```

restrict the flattening by passing column names
```nu
> [[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions --all | last | get versions

```

Flatten inner table
```nu
> { a: b, d: [ 1 2 3 4 ], e: [ 4 3 ] } | flatten d --all
╭───┬───┬───┬───────────╮
│ # │ a │ d │     e     │
├───┼───┼───┼───────────┤
│ 0 │ b │ 1 │ ╭───┬───╮ │
│   │   │   │ │ 0 │ 4 │ │
│   │   │   │ │ 1 │ 3 │ │
│   │   │   │ ╰───┴───╯ │
│ 1 │ b │ 2 │ ╭───┬───╮ │
│   │   │   │ │ 0 │ 4 │ │
│   │   │   │ │ 1 │ 3 │ │
│   │   │   │ ╰───┴───╯ │
│ 2 │ b │ 3 │ ╭───┬───╮ │
│   │   │   │ │ 0 │ 4 │ │
│   │   │   │ │ 1 │ 3 │ │
│   │   │   │ ╰───┴───╯ │
│ 3 │ b │ 4 │ ╭───┬───╮ │
│   │   │   │ │ 0 │ 4 │ │
│   │   │   │ │ 1 │ 3 │ │
│   │   │   │ ╰───┴───╯ │
╰───┴───┴───┴───────────╯

```

Chunks
9c640376 (1st chunk of `commands/docs/flatten.md`)
Title: flatten
Summary
The `flatten` command in Nushell is a filter that flattens tables and lists. When applied to a table, it converts the table into a list of values, effectively removing the structure. It can also be used to flatten inner tables one level out using the `--all` flag. Additionally, you can selectively flatten data by specifying column names as arguments. The command accepts lists and records as input and produces lists and tables as output, respectively. Several examples demonstrate flattening an entire table, accessing the first item of a flattened table, flattening columns containing nested tables, restricting flattening to specific columns, and flattening inner tables within a record. This provides flexibility in restructuring data for further processing or analysis within Nushell pipelines, allowing you to manipulate tabular data and nested structures to achieve the desired data representation.