Head of a list

Keep N items from the start of a list. Default is the first 5. Four Pick modes: First (default, positional head), Random (unbiased sample), Longest (by character count, ties broken by input order), Shortest (by character count). Mirror of Tail of a list.

Input
Ready
Output
Live

Keep N from the start, by position or by shape

First mode (default) takes the first N items preserving order - `Array.slice(0, n)`. If the list has fewer than N items, every item is kept. Random picks N items via a Fisher-Yates shuffle of a copy of the list, then slices the first N - unbiased, not positional.

Longest / Shortest sort the list by character count (descending / ascending) and take the first N. Character count is `String.length` on each line - multi-byte characters count as their UTF-16 code-unit length.

Sorting is stable (ES2019+), so ties preserve original input order. `N` greater than the list length is clamped to the list length. Pair with Tail of a list for the other end - same three non-positional modes, opposite positional default.

How to use head of a list

  1. 1Paste your list into the input panel
  2. 2Set `Keep N` to the number of items you want (default `5`)
  3. 3Pick mode: First (default positional), Random, Longest, or Shortest
  4. 4Output updates live; `N` past the list length keeps everything
  5. 5For the last-N side, use Tail of a list

Keyboard shortcuts

Drive ListShift without touching the mouse.

Shortcut Action
Ctrl ZUndo last input change
Ctrl Shift ZRedo
Ctrl Shift EnterToggle fullscreen focus on the editor
EscExit fullscreen
Ctrl KOpen the command palette to jump to any tool
Ctrl SSave current pipeline draft Plus
Ctrl PRun a saved pipeline Plus

What this tool actually does

Four ways to pick N items from a list.

First mode (default): positional head

`Array.slice(0, n)` gives the first N items in their original order. The common case when "head" means "first few" in a log or chronological list.

Random mode: unbiased sample of N

Fisher-Yates shuffles a copy of the list, then takes the first N. Each item has equal probability; no item repeats. Re-run gives different results (no seed).

Longest mode: N items with most characters

Sorts by `String.length` descending and takes the top N. Ties preserve input order.

Shortest mode: N items with fewest characters

Sort ascending by length, take top N. Ties preserve input order.

N clamped to list length

If your list has 3 items and `Keep N: 10`, you get all 3 - no padding, no error. Empty input returns empty output.

Worked example

Defaults `Keep N: 5, Pick: First` on a 7-item list → first 5 items in order.

Input
Apple
Banana
Cherry
Date
Elderberry
Fig
Grape
Output
Apple
Banana
Cherry
Date
Elderberry

Settings reference

How each option shapes the output using the sample above.

Setting What it does Effect on the sample
`Keep N: 5, Pick: First` (defaults) First 5 items, original order `Apple, Banana, Cherry, Date, Elderberry`
`Keep N: 3, Pick: First` First 3 items `Apple, Banana, Cherry`
`Keep N: 3, Pick: Random` Unbiased random sample of 3 items Different 3 items each run (no seed)
`Keep N: 2, Pick: Longest` 2 items with most characters `Elderberry` (10 chars), then next-longest (`Banana` / `Cherry` tied at 6 - first wins by input order → `Banana`)
`Keep N: 2, Pick: Shortest` 2 items with fewest characters `Fig` (3 chars), then `Apple` (5)

FAQ

What happens if the list has fewer than N items?
Every item is kept. No padding, no error. If your list has 3 items and `Keep N: 10`, the output is those 3 items.
Is Random mode reproducible?
No - there is no seed input. Each run uses fresh `Math.random()` calls.
How are ties broken in Longest / Shortest?
By original input order (stable sort). Two items with the same character count appear in the order they appeared in the input.
How is this different from Truncate?
Truncate is fixed to the `First` mode - it always keeps the first N. This tool adds Random / Longest / Shortest alongside First for more flexible selection.
Does First mode count blank lines?
Yes - every line counts. `splitLines(input)` does not filter blanks before the slice. Chain Remove empty lines first to exclude blanks from the count.