Search

Dark theme | Light theme

July 19, 2026

Nushell Niceties: Finding Same Elements In Lists Or Tables

The intersect command can be used on lists and tables to find the same elements that are in the input list or table and a provided list or table. The input is passed to the command and the other list or table is an argument for the intersect command.

The following examples shows usages of the intersect command:

use std/assert

# intersect return a new list with all elements found in
# the input list and the other list.
assert equal ([1 2 3 4 5] | intersect [2 4 6 8]) [2 4]

# An empty list is returned when no element is
# in both lists
assert equal ([a b c] | intersect [d e f]) []


# intersect can be used with tables as well
(assert equal
  ([[lower upper]; [a A] [b B] [c C]] | intersect [[lower upper]; [c C] [d D]])
  [[lower upper]; [c C]])
(assert equal
  ([{a: A} {b: B} {c: C}] | intersect [{c: C} {d: D}])
  [{c: C}])

(assert equal
  ([[lower upper]; [a A] [b B] [c C]] | intersect [[lower upper]; [d D] [e E]])
  [])

Written with Nushell 0.114.1.