combinations

For filters.

Generates all combinations of size k from the input list.

Command Type

built-in

Signature

> combinations {flags} (k)

Parameters

  • k: The size of each combination (k).

Input/output types:

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

Examples

Generate all 2-combinations

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

Generate combinations of lists

> [[a] [b] [c]] | combinations 2
╭───┬───────────────────╮
│ 0 │ ╭───┬───────────╮ │
│   │ │ 0 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ a │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ │ 1 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ b │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ ╰───┴───────────╯ │
│ 1 │ ╭───┬───────────╮ │
│   │ │ 0 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ a │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ │ 1 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ c │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ ╰───┴───────────╯ │
│ 2 │ ╭───┬───────────╮ │
│   │ │ 0 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ b │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ │ 1 │ ╭───┬───╮ │ │
│   │ │   │ │ 0 │ c │ │ │
│   │ │   │ ╰───┴───╯ │ │
│   │ ╰───┴───────────╯ │
╰───┴───────────────────╯

k > n yields an empty list

> [1 2] | combinations 3
╭────────────╮
│ empty list │
╰────────────╯

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