

# List length and element access functions

Use these functions to count the elements of a list, and to read one element, a slice, or the elements at the positions that you choose. Positions in a list start at `1`. The [`cardinality` function](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-cardinality) also counts the elements of a list.

## `array_extract` function
<a name="athena-duckdb-functions-list-array-extract"></a>

Returns the element of `value` at position `index` when `value` is a list. Positions start at `1`, and a negative `index` counts back from the end, so `-1` is the last element. When `index` is out of range, the result is `NULL`. If `value` or `index` is `NULL`, the result is `NULL`. To read a list element, you can also use the [`list_extract` function](#athena-duckdb-functions-list-list-extract) or a subscript, as in `{{list}}[{{index}}]`.

For a struct with named fields, pass the field name as `entry` to get that field. For an unnamed struct, such as one built with `row`, pass the field position as `index`.

`array_extract` also accepts a `VARCHAR` string as `value`, and then returns the character at position `index` as a one-character `VARCHAR`. Positions in a string follow the same rules as in a list, but when `index` is out of range, the string form returns an empty string, not `NULL`.

### Syntax
<a name="athena-duckdb-functions-list-array-extract-syntax"></a>

```sql
array_extract({{value}}, {{index}})
array_extract({{struct}}, {{entry}})
```

### Arguments
<a name="athena-duckdb-functions-list-array-extract-arguments"></a>

The `array_extract` function takes the following arguments:

**{{value}}**  
The list, the unnamed `STRUCT`, or the `VARCHAR` string to read from.

**{{index}}**  
The position to read, as a `BIGINT` value, starting at `1`.

**{{struct}}**  
The struct to read a field from, as a `STRUCT` value with named fields.

**{{entry}}**  
The name of the field to return, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-list-array-extract-return-type"></a>

The type of the selected item: the element type for a list (for example, `INTEGER` for an `INTEGER[]` list), the field type for a struct, or `VARCHAR` for a string.

### Examples
<a name="athena-duckdb-functions-list-array-extract-examples"></a>

The following SQL query returns a list element, and shows that a position past the end of a list returns `NULL`.

```sql
SELECT
    array_extract([10, 20, 30], 2) AS second_element,
    array_extract([10, 20, 30], 5) AS out_of_range
```

The query returns the following result.


| second\_element | out\_of\_range | 
| --- | --- | 
| `20` | `NULL` | 

The following SQL query returns a field of a struct by name.

```sql
SELECT
    array_extract(
        {'id': 1, 'note': 'shipped'}, 'note'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `shipped` | 

The following SQL query returns the second character of a string.

```sql
SELECT array_extract('DuckDB', 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `u` | 

The following SQL query returns the last character of a string, and shows that a position past the end returns an empty string.

```sql
SELECT
    array_extract('DuckDB', -1) AS last_char,
    array_extract(
        'DuckDB', 10
    ) = '' AS out_of_range_is_empty
```

The query returns the following result.


| last\_char | out\_of\_range\_is\_empty | 
| --- | --- | 
| `B` | `true` | 

### Considerations
<a name="athena-duckdb-functions-list-array-extract-considerations"></a>

For a struct with named fields, a field name that the struct does not have causes an error. It does not return `NULL`.

For an unnamed struct, `index` must be from `1` to the number of fields. Negative positions cause an error.

## `array_length` function
<a name="athena-duckdb-functions-list-array-length"></a>

Returns the number of elements in `list`. `NULL` elements are counted. For an empty list, the result is `0`, and if `list` is `NULL`, the result is `NULL`.

For a list of lists, the result is the number of inner lists, not the total number of elements. With `dimension`, you can read the size of one level of a nested `ARRAY` value.

For a list, `array_length` returns the same result as `len`.

### Syntax
<a name="athena-duckdb-functions-list-array-length-syntax"></a>

```sql
array_length({{list}} [, {{dimension}} ])
```

### Arguments
<a name="athena-duckdb-functions-list-array-length-arguments"></a>

The `array_length` function takes the following arguments:

**{{list}}**  
The list or `ARRAY` value to measure.

**{{dimension}}**  
The level to measure, as a `BIGINT` value, where `1` is the outermost level. For a nested `ARRAY` type such as `INTEGER[3][2]`, which holds two arrays of three `INTEGER` values each, `1` returns `2` and `2` returns `3`. The default is `1`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-list-array-length-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-list-array-length-examples"></a>

The following SQL query returns the number of elements in a list.

```sql
SELECT array_length([1, 2, 3]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query counts the elements of a list that has a `NULL` element, of an empty list, and of a list of two lists.

```sql
SELECT
    array_length([1, NULL, 3]) AS with_null,
    array_length(CAST([] AS INTEGER[])) AS empty_list,
    array_length([[1, 2], [3]]) AS nested_list
```

The query returns the following result.


| with\_null | empty\_list | nested\_list | 
| --- | --- | --- | 
| `3` | `0` | `2` | 

The following SQL query measures both levels of an `INTEGER[3][2]` value, an `ARRAY` of two `ARRAY` values of three elements each.

```sql
SELECT
    array_length(
        [[1, 2, 3], [4, 5, 6]]::INTEGER[3][2], 1
    ) AS outer_size,
    array_length(
        [[1, 2, 3], [4, 5, 6]]::INTEGER[3][2], 2
    ) AS inner_size
```

The query returns the following result.


| outer\_size | inner\_size | 
| --- | --- | 
| `2` | `3` | 

### Considerations
<a name="athena-duckdb-functions-list-array-length-considerations"></a>

For a list, `1` is the only `dimension` that you can pass. Any other value makes the query fail, even for a list of lists. For a nested `ARRAY`, a `dimension` greater than the number of levels makes the query fail.

`array_length` does not accept a string. To get the number of characters in a string, use the [`length` function](#athena-duckdb-functions-list-length).

## `length` function
<a name="athena-duckdb-functions-list-length"></a>

Returns the length of `value`. For a `VARCHAR` value, this is the number of characters (Unicode code points), not bytes. For a `LIST` or `ARRAY`, it is the number of top-level elements, including `NULL` elements. For a `BIT` value, it is the number of bits. If `value` is `NULL`, the result is `NULL`.

To count the bytes in a string, use the [`strlen` function](duckdb-functions-string-unicode-characters.md#athena-duckdb-functions-string-strlen). To count the bytes in a `BLOB`, use `octet_length`. To count grapheme clusters, the characters as a reader sees them, use the [`length_grapheme` function](duckdb-functions-string-unicode-characters.md#athena-duckdb-functions-string-length-grapheme).

`char_length`, `character_length`, and `len` are aliases of `length`.

### Syntax
<a name="athena-duckdb-functions-list-length-syntax"></a>

```sql
length({{value}})
```

### Arguments
<a name="athena-duckdb-functions-list-length-arguments"></a>

The `length` function takes the following argument:

**{{value}}**  
The value to measure, as a `VARCHAR`, `LIST`, `ARRAY`, or `BIT` value.

### Return type
<a name="athena-duckdb-functions-list-length-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-list-length-examples"></a>

The following SQL query returns the number of elements in a list.

```sql
SELECT length([1, 2, 3]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query returns the number of characters in a string, and uses the `len` alias to count the elements of a list. The `NULL` element is counted.

```sql
SELECT
    length('Hello') AS string_length,
    len([1, NULL, 3]) AS list_length
```

The query returns the following result.


| string\_length | list\_length | 
| --- | --- | 
| `5` | `3` | 

The following SQL query returns the number of bits in a bitstring. Casting the `TINYINT` value `42` to `BIT` produces 8 bits.

```sql
SELECT length(42::TINYINT::BIT) AS result
```

The query returns the following result.


| result | 
| --- | 
| `8` | 

The following SQL query shows that an empty string and an empty list have a length of `0`, and that a `NULL` input returns `NULL`.

```sql
SELECT
    length('') AS empty_string,
    length([]) AS empty_list,
    length(NULL::VARCHAR) AS null_input
```

The query returns the following result.


| empty\_string | empty\_list | null\_input | 
| --- | --- | --- | 
| `0` | `0` | `NULL` | 

### Considerations
<a name="athena-duckdb-functions-list-length-considerations"></a>

`length` does not accept `BLOB` or numeric values, and such a call fails. Use `octet_length` for a `BLOB`, and cast a number to `VARCHAR` to count its characters.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-length-differences-from-engine-version-3"></a>

To count the elements of an array, Athena engine version 3 queries use `cardinality({{array}})`. In DuckDB, use `len({{list}})` or `length({{list}})`.

In Athena engine version 3, `length` also accepts a `VARBINARY` value and returns its number of bytes. In DuckDB, `length` does not accept a `BLOB` value. Use `octet_length` instead.

## `list_any_value` function
<a name="athena-duckdb-functions-list-list-any-value"></a>

Applies the `any_value` aggregate function to the elements of `list`, which returns the first element that is not `NULL`. It is the same as `list_aggregate({{list}}, 'any_value')`.

If `list` is empty, is `NULL`, or has only `NULL` elements, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-list-list-any-value-syntax"></a>

```sql
list_any_value({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-any-value-arguments"></a>

The `list_any_value` function takes the following argument:

**{{list}}**  
The list to return an element from, as a `LIST` or `ARRAY` value of any element type.

### Return type
<a name="athena-duckdb-functions-list-list-any-value-return-type"></a>

The element type of `list`, for example `INTEGER` for an `INTEGER[]` list.

### Examples
<a name="athena-duckdb-functions-list-list-any-value-examples"></a>

The following SQL query returns the first element of a list.

```sql
SELECT list_any_value([3, 3, 9]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query skips a leading `NULL` element and returns the first element that is not `NULL`.

```sql
SELECT list_any_value([NULL, 7, 9]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `7` | 

The following SQL query passes a list whose elements are all `NULL`, so the result is `NULL`.

```sql
SELECT list_any_value([NULL, NULL]::INTEGER[]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

## `list_count` function
<a name="athena-duckdb-functions-list-list-count"></a>

Counts the elements of `list` that are not `NULL`. Duplicate values each count. If `list` is empty, the result is `0`. If `list` is `NULL`, the result is `NULL`.

To count every element, including `NULL` elements, use `len`. `list_count({{list}})` returns the same result as `list_aggregate({{list}}, 'count')`.

### Syntax
<a name="athena-duckdb-functions-list-list-count-syntax"></a>

```sql
list_count({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-count-arguments"></a>

The `list_count` function takes the following argument:

**{{list}}**  
The list whose elements you want to count, of any element type.

### Return type
<a name="athena-duckdb-functions-list-list-count-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-list-list-count-examples"></a>

The following SQL query counts the elements of a list. Both copies of 3 are counted.

```sql
SELECT list_count([3, 3, 9]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query compares `list_count` and `len` on a list that holds a `NULL` element.

```sql
SELECT
    list_count([1, NULL, 3]) AS non_null_elements,
    len([1, NULL, 3]) AS all_elements
```

The query returns the following result.


| non\_null\_elements | all\_elements | 
| --- | --- | 
| `2` | `3` | 

The following SQL query shows that an empty list returns `0`.

```sql
SELECT list_count(CAST([] AS INTEGER[])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-list-list-count-considerations"></a>

`list_count` does not count the occurrences of one value. To count the elements that equal a value, filter the list first, as in `len(list_filter({{list}}, lambda x: x = {{value}}))`.

## `list_extract` function
<a name="athena-duckdb-functions-list-list-extract"></a>

Returns the element of `list` at position `index`. Positions start at `1`, and a negative `index` counts from the end, so `-1` returns the last element. If `index` is `0` or points past either end of the list, the result is `NULL`. If `list` or `index` is `NULL`, the result is `NULL`.

When the first argument is a string, `list_extract` returns the character at position `index` as a `VARCHAR` value. For a position outside the string, the result is an empty string, not `NULL`.

`list_element` is an alias of `list_extract`.

### Syntax
<a name="athena-duckdb-functions-list-list-extract-syntax"></a>

```sql
list_extract({{list}}, {{index}})
list_extract({{string}}, {{index}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-extract-arguments"></a>

The `list_extract` function takes the following arguments:

**{{list}}**  
The list to read an element from.

**{{string}}**  
The string to read a character from, as a `VARCHAR` value.

**{{index}}**  
The position of the element or character to return, as a `BIGINT` value. `1` is the first element, and `-1` is the last.

### Return type
<a name="athena-duckdb-functions-list-list-extract-return-type"></a>

The element type of `list`, such as `INTEGER` for an `INTEGER[]` list. For a string, the result is a `VARCHAR` value.

### Examples
<a name="athena-duckdb-functions-list-list-extract-examples"></a>

The following SQL query returns the third element of a list.

```sql
SELECT list_extract([4, 5, 6], 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `6` | 

The following SQL query uses a negative index to return the last element, and shows that index `0` and an index past the end return `NULL`.

```sql
SELECT
    list_extract([4, 5, 6], -1) AS last_element,
    list_extract([4, 5, 6], 0) AS index_zero,
    list_extract([4, 5, 6], 7) AS past_end
```

The query returns the following result.


| last\_element | index\_zero | past\_end | 
| --- | --- | --- | 
| `6` | `NULL` | `NULL` | 

The following SQL query uses the `list_element` alias to return the second character of a string. It also shows that a position past the end of a string returns an empty string.

```sql
SELECT
    list_element('DuckDB', 2) AS second_character,
    list_extract('DuckDB', 10) = '' AS past_end_is_empty
```

The query returns the following result.


| second\_character | past\_end\_is\_empty | 
| --- | --- | 
| `u` | `true` | 

### Considerations
<a name="athena-duckdb-functions-list-list-extract-considerations"></a>

`index` must be an integer. A value such as `1.7` causes an error and is not rounded.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-extract-differences-from-engine-version-3"></a>

In Athena engine version 3, you get an array element with `element_at({{array}}, {{index}})`. In DuckDB, `element_at` accepts only a map, so that call fails on an array. Use `list_extract({{array}}, {{index}})` or `{{array}}[{{index}}]` instead.

## `list_first` function
<a name="athena-duckdb-functions-list-list-first"></a>

Returns the first element of `list`. The function does not skip `NULL` elements, so if the first element is `NULL`, the result is `NULL`. If `list` is empty or `NULL`, the result is `NULL`.

`list_first({{list}})` returns the same result as `list_aggregate({{list}}, 'first')`. To get the first element that is not `NULL`, use the [`list_any_value` function](#athena-duckdb-functions-list-list-any-value). To get the last element, use the [`list_last` function](#athena-duckdb-functions-list-list-last).

### Syntax
<a name="athena-duckdb-functions-list-list-first-syntax"></a>

```sql
list_first({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-first-arguments"></a>

The `list_first` function takes the following argument:

**{{list}}**  
The list to read the first element from, with elements of any type.

### Return type
<a name="athena-duckdb-functions-list-list-first-return-type"></a>

The element type of `list`, for example `INTEGER` for an `INTEGER[]` list.

### Examples
<a name="athena-duckdb-functions-list-list-first-examples"></a>

The following SQL query returns the first element of a list.

```sql
SELECT list_first([3, 3, 9]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query shows that `list_first` returns `NULL` when the first element is `NULL`. `list_any_value` skips the `NULL` element and returns the first element that is not `NULL`.

```sql
SELECT
    list_first([NULL, 1, 2]) AS first_element,
    list_any_value([NULL, 1, 2]) AS first_non_null
```

The query returns the following result.


| first\_element | first\_non\_null | 
| --- | --- | 
| `NULL` | `1` | 

## `list_last` function
<a name="athena-duckdb-functions-list-list-last"></a>

Returns the last element of `list`. The function does not skip `NULL` elements, so if the last element is `NULL`, the result is `NULL`. If `list` is empty or `NULL`, the result is `NULL`.

`list_last({{list}})` returns the same result as `list_aggregate({{list}}, 'last')`. To get the first element, use the [`list_first` function](#athena-duckdb-functions-list-list-first).

### Syntax
<a name="athena-duckdb-functions-list-list-last-syntax"></a>

```sql
list_last({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-last-arguments"></a>

The `list_last` function takes the following argument:

**{{list}}**  
The list to read the last element from, with elements of any type.

### Return type
<a name="athena-duckdb-functions-list-list-last-return-type"></a>

The element type of `list`, for example `INTEGER` for an `INTEGER[]` list.

### Examples
<a name="athena-duckdb-functions-list-list-last-examples"></a>

The following SQL query returns the last element of a list.

```sql
SELECT list_last([3, 3, 9]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `9` | 

The following SQL query returns `NULL` because the last element is `NULL`.

```sql
SELECT list_last([1, 2, NULL]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query returns the last element that is not `NULL`. The `list_filter` function first removes the `NULL` elements: the lambda function `lambda x: x IS NOT NULL` keeps each element `x` that is not `NULL`.

```sql
SELECT
    list_last(
        list_filter(
            [1, 2, NULL], lambda x: x IS NOT NULL
        )
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

## `list_select` function
<a name="athena-duckdb-functions-list-list-select"></a>

Returns a list with one element for each position in `index_list`: the element of `value_list` at that 1-based position. Positions can repeat and can appear in any order, so you can use `list_select` to pick, reorder, or repeat elements.

A position that is `0`, negative, or greater than the length of `value_list` gives a `NULL` element in the result. Negative positions do not count from the end of the list. If either list is `NULL`, the result is `NULL`.

`array_select` is an alias of `list_select`. To keep the elements that match a list of `BOOLEAN` values instead, use the [`list_where` function](#athena-duckdb-functions-list-list-where).

### Syntax
<a name="athena-duckdb-functions-list-list-select-syntax"></a>

```sql
list_select({{value_list}}, {{index_list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-select-arguments"></a>

The `list_select` function takes the following arguments:

**{{value\_list}}**  
The list to take elements from, of any element type.

**{{index\_list}}**  
The positions of the elements to return, as a `BIGINT[]` list of integers. Lists of other integer types are converted. The list cannot contain `NULL` elements.

### Return type
<a name="athena-duckdb-functions-list-list-select-return-type"></a>

A list of the same type as `value_list`, for example `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-list-select-examples"></a>

The following SQL query returns the first and fourth elements of a list.

```sql
SELECT list_select([10, 20, 30, 40], [1, 4]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[10, 40]` | 

The following SQL query reorders the elements and repeats one of them.

```sql
SELECT list_select(['a', 'b', 'c'], [3, 1, 1]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[c, a, a]` | 

The following SQL query uses the `array_select` alias with a position past the end of the list, which gives a `NULL` element.

```sql
SELECT array_select([10, 20, 30, 40], [2, 5]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[20, NULL]` | 

The following SQL query sorts one list by the values of another. `list_grade_up([2, 3, 1])` returns `[3, 1, 2]`, the positions that put `[2, 3, 1]` in ascending order.

```sql
SELECT
    list_select(
        ['b', 'c', 'a'], list_grade_up([2, 3, 1])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[a, b, c]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-select-considerations"></a>

If `index_list` contains a `NULL` element, the query fails.

## `list_slice` function
<a name="athena-duckdb-functions-list-list-slice"></a>

Returns the elements of `list` from position `begin` through position `end`. Positions are 1-based, and the result includes both `begin` and `end`. A negative position counts from the end of the list, so `-1` is the last element. With `step`, the function returns every `step`-th element, starting at `begin`. A negative `step` moves backward from `begin` to `end`.

Positions outside the list are limited to the list, so the result can be shorter than you ask for, or empty. A `begin` of `0` works like `1`. If `begin` or `end` is `NULL`, the result is `NULL`.

You can also pass a `VARCHAR` value to get a substring. The string form does not take `step`. `array_slice` is an alias of `list_slice`.

### Syntax
<a name="athena-duckdb-functions-list-list-slice-syntax"></a>

```sql
list_slice({{list}}, {{begin}}, {{end}} [, {{step}} ])
list_slice({{string}}, {{begin}}, {{end}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-slice-arguments"></a>

The `list_slice` function takes the following arguments:

**{{list}}**  
The list to take elements from, of any element type.

**{{string}}**  
The string to take characters from, as a `VARCHAR` value.

**{{begin}}**  
The position of the first element or character to return, as a `BIGINT` value. A negative value counts from the end.

**{{end}}**  
The position of the last element or character to return, as a `BIGINT` value. A negative value counts from the end. To slice to the end, pass `-1`.

**{{step}}**  
The distance between the positions of the returned elements, as a `BIGINT` other than `0`. The default is `1`. You can pass it only for lists. This argument is optional.

### Return type
<a name="athena-duckdb-functions-list-list-slice-return-type"></a>

The type of the first argument: a list of the same type as `list`, such as `INTEGER[]`, or `VARCHAR` for a string.

### Examples
<a name="athena-duckdb-functions-list-list-slice-examples"></a>

The following SQL query returns the elements from position 2 through position 3.

```sql
SELECT list_slice([4, 5, 6], 2, 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[5, 6]` | 

The following SQL query returns every second element from position 1 through position 3.

```sql
SELECT list_slice([4, 5, 6], 1, 3, 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[4, 6]` | 

The following SQL query uses negative positions to return the last two elements.

```sql
SELECT list_slice([1, 2, 3, 4, 5], -2, -1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[4, 5]` | 

The following SQL query uses the `array_slice` alias to return the third and fourth characters of a string.

```sql
SELECT array_slice('DuckDB', 3, 4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `ck` | 

### Considerations
<a name="athena-duckdb-functions-list-list-slice-considerations"></a>

If `begin` or `end` is `NULL`, the result is `NULL`, not the rest of the list. To slice to the end, pass `-1` as `end`.

A `step` of `0` makes the query fail, and so does a `step` with a string.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-slice-differences-from-engine-version-3"></a>

In Athena engine version 3, the third argument of `slice({{array}}, {{start}}, {{length}})` is the number of elements to return. DuckDB has no `slice` function. In DuckDB, the third argument of `list_slice` is the position of the last element to return. To rewrite `slice({{array}}, {{start}}, {{length}})`, use `list_slice({{array}}, {{start}}, {{start}} + {{length}} - 1)`. When `start` is negative and the slice reaches the end of the array, use `-1` as the end position instead.

## `list_where` function
<a name="athena-duckdb-functions-list-list-where"></a>

Compares `value_list` and `mask_list` position by position, and returns, in order, the elements of `value_list` whose matching element of `mask_list` is `true`. Elements whose matching mask element is `false` are left out.

If `mask_list` is shorter than `value_list`, the elements of `value_list` past the end of the mask are left out. If `mask_list` is longer and is `true` at a position past the end of `value_list`, the result has a `NULL` element for that position. If either list is `NULL`, the result is `NULL`.

`array_where` is an alias of `list_where`. To keep the elements that meet a condition, use the [`list_filter` function](duckdb-functions-list-lambda-functions.md#athena-duckdb-functions-list-list-filter), or build the mask with `list_transform`.

### Syntax
<a name="athena-duckdb-functions-list-list-where-syntax"></a>

```sql
list_where({{value_list}}, {{mask_list}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-where-arguments"></a>

The `list_where` function takes the following arguments:

**{{value\_list}}**  
The list to take elements from, as a `LIST` value.

**{{mask\_list}}**  
The mask, as a `LIST` of `BOOLEAN` values. Element `n` of `mask_list` decides whether element `n` of `value_list` is in the result.

### Return type
<a name="athena-duckdb-functions-list-list-where-return-type"></a>

A `LIST` of the same type as `value_list`, such as `INTEGER[]`.

### Examples
<a name="athena-duckdb-functions-list-list-where-examples"></a>

The following SQL query keeps the first and the last element of a list.

```sql
SELECT
    list_where(
        [10, 20, 30, 40], [true, false, false, true]
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[10, 40]` | 

The following SQL query builds the mask from a second list with `list_transform`, and keeps the letters whose matching number is greater than 3.

```sql
SELECT
    list_where(
        ['a', 'b', 'c'],
        list_transform([1, 5, 9], lambda x: x > 3)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[b, c]` | 

The following SQL query uses a mask that is shorter than the list and a mask that is longer than the list. The shorter mask leaves out the last two elements, and the longer mask adds a `NULL` element.

```sql
SELECT
    list_where([10, 20, 30], [true]) AS short_mask,
    list_where(
        [10, 20], [true, true, true]
    ) AS long_mask
```

The query returns the following result.


| short\_mask | long\_mask | 
| --- | --- | 
| `[10]` | `[10, 20, NULL]` | 

The following SQL query replaces the `NULL` element of a mask with `false` before it calls `list_where`.

```sql
SELECT
    list_where(
        [10, 20, 30],
        list_transform(
            [true, NULL, true],
            lambda b: coalesce(b, false)
        )
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[10, 30]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-where-considerations"></a>

If `mask_list` contains a `NULL` element, the query fails. To treat `NULL` as `false`, replace it first, as in `list_where({{value_list}}, list_transform({{mask_list}}, lambda b: coalesce(b, false)))`.

`mask_list` must be a list of `BOOLEAN` values. If you pass a list of the numbers `1` and `0`, the query fails.