permutations

For filters.

Generates all permutations of the input list.

Command Type

built-in

Signature

> permutations {flags}

Input/output types:

input output
list<any> list<list<any>>
table table

Examples

Generate all permutations

> [1 2 3] | permutations
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 0 │ 1 │ │
│   │ │ 1 │ 2 │ │
│   │ │ 2 │ 3 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 0 │ 2 │ │
│   │ │ 1 │ 1 │ │
│   │ │ 2 │ 3 │ │
│   │ ╰───┴───╯ │
│ 2 │ ╭───┬───╮ │
│   │ │ 0 │ 3 │ │
│   │ │ 1 │ 1 │ │
│   │ │ 2 │ 2 │ │
│   │ ╰───┴───╯ │
│ 3 │ ╭───┬───╮ │
│   │ │ 0 │ 1 │ │
│   │ │ 1 │ 3 │ │
│   │ │ 2 │ 2 │ │
│   │ ╰───┴───╯ │
│ 4 │ ╭───┬───╮ │
│   │ │ 0 │ 2 │ │
│   │ │ 1 │ 3 │ │
│   │ │ 2 │ 1 │ │
│   │ ╰───┴───╯ │
│ 5 │ ╭───┬───╮ │
│   │ │ 0 │ 3 │ │
│   │ │ 1 │ 2 │ │
│   │ │ 2 │ 1 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

Generate permutations of two elements

> [1 2] | permutations
╭───┬───────────╮
│ 0 │ ╭───┬───╮ │
│   │ │ 0 │ 1 │ │
│   │ │ 1 │ 2 │ │
│   │ ╰───┴───╯ │
│ 1 │ ╭───┬───╮ │
│   │ │ 0 │ 2 │ │
│   │ │ 1 │ 1 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯

Empty list yields no permutations

> [] | permutations
╭────────────╮
│ empty list │
╰────────────╯

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.