Search

Dark theme | Light theme

June 13, 2025

Nushell Niceties: Adding Indexes To Lists

The Nushell command enumerate adds an index value to each item in a list. The index value is stored in an extra column named index. You can use this column to filter data based on the index value.

In the following examples you can see several use cases of the enumerate command:

use std/assert

# enumerate adds a column with the name index
# to a list of items.
assert equal ([a b c] | enumerate) [[index item]; [0 a] [1 b] [2 c]]

# Works also with for example numbers.
assert equal ([0 1 2] | enumerate) [[index item]; [0 0] [1 1] [2 2]]

# With a table as input the output is a new table with columns index and item.
let input = [[name age]; [Alice 29] [Bob 42]]
assert equal ($input | enumerate) [[index item]; [0 {name: Alice age: 29}] [1 {name: Bob age: 42}]]

# A single record shows the column names index and item.
assert equal ([a b c] | enumerate | first) {index: 0, item: a}

# You can use the new index column to filter data.
assert equal ([a b c] | enumerate | where index > 1) [[index item]; [2 c]]

Written with Nushell 0.105.1.