# Lists

Lists in RuLA are ordered collections of values. They can contain any type of value: strings, numbers, other lists, or dictionaries.

## Creating Lists

Use square brackets with comma-separated values:

```
['apple', 'banana', 'cherry']           # List of strings
[1, 2, 3, 4, 5]                          # List of numbers
[]                                       # Empty list
['text', 42, {'key': 'value'}]          # Mixed types
```

## Accessing List Elements

Access elements by index (0-based):

```
fruits = ['apple', 'banana', 'cherry']
fruits[0]           # Returns 'apple'
fruits[2]           # Returns 'cherry'
```

## List Manipulation

### Adding Elements

```
APPEND([1, 2], 3)             # Returns [1, 2, 3]
CONCAT([1, 2], [3, 4])        # Concatenation: [1, 2, 3, 4]
```

### Filtering

```
numbers = [1, 2, 3, 4, 5]
FILTER(numbers, x => x > 3)             # Returns [4, 5]
FILTER_FALSE(true, false, true, false)  # Returns [true, true]
```

### Transforming (Mapping)

```
numbers = [1, 2, 3]
MAP(numbers, x => x * 2)        # Returns [2, 4, 6]
```

### Sorting and Reversing

```
SORT([3, 1, 2])                 # Returns [1, 2, 3]
SORT(['banana', 'apple'])       # Returns ['apple', 'banana']
REVERSE([1, 2, 3])              # Returns [3, 2, 1]
```

### Getting Unique Elements

```
SET([1, 2, 2, 3, 3, 3])         # Returns [1, 2, 3]
```

### Flattening Nested Lists

```
FLATTEN([[1, 2], [3, 4]])       # Returns [1, 2, 3, 4]
```

## Useful List Functions

| Function                                                                      | Description                            | Example                                          |
| ----------------------------------------------------------------------------- | -------------------------------------- | ------------------------------------------------ |
| [`COUNT`](/rule-language/operators/list-operators/count.md)                   | Number of elements                     | `COUNT([1, 2, 3])` → `3`                         |
| [`SORT`](/rule-language/operators/list-operators/sort.md)                     | Sort in ascending order                | `SORT([3, 1, 2])` → `[1, 2, 3]`                  |
| [`REVERSE`](/rule-language/operators/list-operators/reverse.md)               | Reverse the order                      | `REVERSE([1, 2, 3])` → `[3, 2, 1]`               |
| [`FIRST_ELEMENT`](/rule-language/operators/list-operators/first_element.md)   | Get first element                      | `FIRST_ELEMENT([1, 2, 3])` → `1`                 |
| [`LAST_ELEMENT`](/rule-language/operators/list-operators/last_element.md)     | Get last element                       | `LAST_ELEMENT([1, 2, 3])` → `3`                  |
| [`APPEND`](/rule-language/operators/list-operators/append.md)                 | Add element to end                     | `APPEND([1, 2], 3)` → `[1, 2, 3]`                |
| [`INDEX_OF`](/rule-language/operators/list-operators/index_of.md)             | Find position (0-based)                | `INDEX_OF(['a', 'b'], 'b')` → `1`                |
| [`SET`](/rule-language/operators/list-operators/set.md)                       | Remove duplicates                      | `SET([1, 1, 2])` → `[1, 2]`                      |
| [`FLATTEN`](/rule-language/operators/list-operators/flatten.md)               | Flatten one level                      | `FLATTEN([[1], [2]])` → `[1, 2]`                 |
| [`UNION(list1, list2)`](/rule-language/operators/list-operators/union.md)     | Combine unique elements                | `UNION([1, 2], [2, 3])` → `[1, 2, 3]`            |
| [`INTERSECTION`](/rule-language/operators/list-operators/intersection.md)     | Common elements                        | `INTERSECTION([1, 2], [2, 3])` → `[2]`           |
| [`SET_DIFFERENCE`](/rule-language/operators/list-operators/set_difference.md) | Elements in list1 not in list2         | `SET_DIFFERENCE([1, 2], [2, 3])` → `[1]`         |
| [`SUM`](/rule-language/operators/mathematical-operators/sum.md)               | Sum of numbers                         | `SUM([1, 2, 3])` → `6`                           |
| [`AVG`](/rule-language/operators/mathematical-operators/avg.md)               | Average of numbers                     | `AVG([1, 2, 3])` → `2`                           |
| [`MIN`](/rule-language/operators/mathematical-operators/min.md)               | Minimum value                          | `MIN([3, 1, 2])` → `1`                           |
| [`MAX`](/rule-language/operators/mathematical-operators/max.md)               | Maximum value                          | `MAX([3, 1, 2])` → `3`                           |
| [`CONCAT`](/rule-language/operators/string-operators/concat.md)               | Join all strings                       | `CONCAT(['a', 'b'])` → `"ab"`                    |
| [`JOIN`](/rule-language/operators/string-operators/join.md)                   | Join with separator                    | `JOIN(['a', 'b'], '-')` → `"a-b"`                |
| [`MAP`](/rule-language/operators/list-operators/map-1.md)                     | Transform each element                 | `MAP([1, 2], x => x * 2)` → `[2, 4]`             |
| [`FILTER`](/rule-language/operators/list-operators/filter-1.md)               | Keep matching elements                 | `FILTER([1, 2, 3], x => x > 1)` → `[2, 3]`       |
| [`ALL`](/rule-language/operators/list-operators/all.md)                       | All values true?                       | `ALL([true, true, false])` → `False`             |
| [`ANY`](/rule-language/operators/list-operators/any.md)                       | Any value true?                        | `ANY([true, true, false])` → `True`              |
| [`SOME`](/rule-language/operators/list-operators/some.md)                     | Any value true? (alias for `ANY`)      | `SOME([true, false])` → `True`                   |
| [`CUSTOM_MAX`](/rule-language/operators/list-operators/custom_max.md)         | Max element ranked by an ordering list | `CUSTOM_MAX([red, green], [green, red])` → `red` |

## Checking List Contents

```
fruits = ['apple', 'banana']

fruits contains 'apple'                  # True
fruits contains any ['apple', 'pear']    # True
fruits contains only ['apple', 'banana'] # True

'apple' in fruits               # True
'pear' not in fruits            # True
```

## List Operations on Aggregated Data

```
# Get all values of an information across instances
all_prices = LIST_OF_ANY(order.line_item.price)
SUM(all_prices)
SORT(all_prices)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.atfinity.io/rule-language/data-types/lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
