List.Alternate
ListReturns a list with items selected at alternating intervals based on count, repeatInterval, and offset.
Syntax
List.Alternate(list as list, count as number, optional repeatInterval as nullable number, optional offset as nullable number) as listParameters
| Name | Type | Required | Description |
|---|---|---|---|
list | list | Yes | The source list to select items from. |
count | number | Yes | The number of items to skip in each alternating cycle. |
repeatInterval | nullable number | No | The number of items to keep in each cycle. Defaults to 1. |
offset | nullable number | No | The number of items to skip at the start of the list before the pattern begins. Defaults to 0. |
Return Value
list — A new list containing items selected at alternating intervals.
Remarks
List.Alternate selects items from a list by cycling through a pattern of skipping and keeping. The cycle works as follows: skip count items, then keep repeatInterval items (defaulting to 1 if omitted), and repeat this cycle until the list is exhausted. The optional offset skips that many items from the very start before the pattern begins.
This function is useful for extracting every Nth item from a list, de-interleaving data that alternates between two streams, or sampling at regular intervals. The behavior can feel counterintuitive at first: the count parameter is how many to skip, not how many to keep.
- When repeatInterval is omitted or null, it defaults to 1 (keep one item per cycle).
- When offset is omitted or null, it defaults to 0 (no items skipped before the pattern starts).
- The item at position offset is the first item kept (it is not itself skipped by the offset — the offset skips that many items before the first kept item).
- For simply extracting every Nth item, use List.Alternate(list, N - 1) — skip N-1, keep 1.
Examples
Example 1: Select every other item (skip 1, keep 1)
List.Alternate({1, 2, 3, 4, 5, 6}, 1)Result | |
|---|---|
| 1 | {1, 3, 5} |
Example 2: Select every third item (skip 2, keep 1)
List.Alternate({1, 2, 3, 4, 5, 6, 7, 8, 9}, 2)Result | |
|---|---|
| 1 | {1, 4, 7} |
Example 3: Skip 1, keep 2, with an offset of 1
List.Alternate({1, 2, 3, 4, 5, 6, 7, 8}, 1, 2, 1)Result | |
|---|---|
| 1 | {2, 3, 5, 6, 8} |