# List

> List operations on data values: get, push, insert, remove, slice, sort, unique, flatten, join, range, sum, minimum, maximum, mean, shuffle and more.

Source: https://docs.tune.toys/nodes/data.list/

List reads, edits and summarises lists of values.

## What it does

The `list` input is read as a list (a single value becomes a one-item list). Operation chooses what to do with it. Some operations need an item: it comes from the `b` input or, when `b` is not connected, from the node's B text field, read as JSON when it parses (`72`, `[1, 2]`) and as text otherwise. Index and Count come from the parameters; a connected `index` input overrides Index.

| Operation                                | Result                                                                 |
| ---------------------------------------- | ---------------------------------------------------------------------- |
| Length, First, Last, Get                 | a number or one element (Get uses Index; no value out of range)        |
| Push, Prepend                            | the list with B added at the end or the start                          |
| Pop, Shift                               | the list without its last or first element                             |
| Insert, Set                              | B inserted at Index, or the element at Index replaced by B             |
| Remove                                   | Count elements removed from Index                                      |
| Slice, Take, Drop                        | Count elements from Index, the first Count, or all but the first Count |
| Concatenate                              | the list followed by B                                                 |
| Reverse, Sort ascending, Sort descending | the reordered list                                                     |
| Unique, Compact                          | duplicates removed, or empty values removed                            |
| Flatten                                  | nested lists opened Count levels deep (0 opens all)                    |
| Join                                     | text with B (a comma by default) between elements                      |
| Index of                                 | position of B, or -1                                                   |
| Range                                    | Count numbers starting at Index                                        |
| Sum, Minimum, Maximum, Mean              | a number from the numeric elements                                     |
| Shuffle                                  | the list in a random order set by Seed                                 |

Negative Index counts from the end. Sort puts numbers before text, numbers in numeric order and text by character, and keeps equal elements in their original order. Lists are limited to 4 096 items.

## When to use it

Use it to keep a list of notes or settings, to pick one element, to tidy imported data (Unique, Compact, Sort) or to reduce a list to one number (Sum, Mean, Maximum). Use [String](https://docs.tune.toys/nodes/data.string/) Split to turn text into a list and Join here to turn it back.

## Ports

<!-- ports:start -->

| Port    | Direction | Signal     | Lanes |
| ------- | --------- | ---------- | ----- |
| `list`  | Input     | Data (any) | 1     |
| `b`     | Input     | Data (any) | 1     |
| `index` | Input     | Data (any) | 1     |
| `value` | Output    | Data (any) | 1     |

<!-- ports:end -->

## Parameters

<!-- parameters:start -->

| Parameter | Label     | Range         | Default | Unit | Scale  | Notes                                                                                                                                                                                                                                                                                                                                                                           |
| --------- | --------- | ------------- | ------- | ---- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `op`      | Operation | 0 to 28       | 0       |      | linear | List operation; b and index are its arguments. Choices: 0 Length, 1 Get, 2 First, 3 Last, 4 Push, 5 Prepend, 6 Pop, 7 Shift, 8 Insert, 9 Remove, 10 Set, 11 Slice, 12 Concatenate, 13 Reverse, 14 Sort ascending, 15 Sort descending, 16 Unique, 17 Flatten, 18 Compact, 19 Join, 20 Index of, 21 Range, 22 Sum, 23 Minimum, 24 Maximum, 25 Mean, 26 Shuffle, 27 Take, 28 Drop. |
| `index`   | Index     | -4096 to 4096 | 0       |      | linear | Position while the index port is unconnected; negative counts from the end. Shown when `op` is 1 or 8 or 9 or 10 or 11 or 21.                                                                                                                                                                                                                                                   |
| `count`   | Count     | 0 to 4096     | 1       |      | linear | Items removed, sliced, flattened, generated, taken or dropped. Shown when `op` is 9 or 11 or 17 or 21 or 27 or 28.                                                                                                                                                                                                                                                              |
| `seed`    | Seed      | 0 to 65535    | 0       |      | linear | Seed for Shuffle, so the order is reproducible. Shown when `op` is 26.                                                                                                                                                                                                                                                                                                          |

<!-- parameters:end -->

<!-- data:start -->

- `b`: text.

<!-- data:end -->

### Operation (`op`)

The list operation, a discrete choice. See the table above for what each one does and which fields it uses.

### Index (`index`)

An element position from -4 096 to 4 096. 0 is the first element and -1 the last. Used by Get, Insert, Remove, Set and Slice, and as the first number for Range. A value on the `index` input replaces it.

### Count (`count`)

A number of elements from 0 to 4 096: how many Remove takes out, Slice keeps (0 keeps the rest), Take keeps, Drop skips and Range makes, or how many levels Flatten opens (0 opens all).

### Seed (`seed`)

From 0 to 65 535. Shuffle mixes the list in an order set by Seed and by how many times it has run, so a recording replays the same way.

## Tips

- To sort a list, use Sort ascending or Sort descending; chain Unique first to drop repeats.
- Sum, Minimum, Maximum and Mean ignore elements that are not numbers, so a mixed list still gives a number.
- Splice is Remove followed by Insert: remove Count elements at Index, then insert B at the same Index.
- Join with B set to `/` turns `["kick", "snare"]` into `kick / snare`.

## See also

[Value](https://docs.tune.toys/nodes/data.value/), [String](https://docs.tune.toys/nodes/data.string/), [Math](https://docs.tune.toys/nodes/data.math/), [Compare](https://docs.tune.toys/nodes/data.compare/).
