

# List element addition and removal functions

Use these functions to add an element to the start or the end of a list, to remove its first or last element, or to change its length.

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

Returns a new list that holds the elements of `list` followed by `element`. The element is added even when it is `NULL`, so the result has one more element than `list`.

If `list` is `NULL`, the function treats it as an empty list and returns a list that holds only `element`. If you pass an `ARRAY` value, the result is a `LIST`.

`array_append` returns the same result as `list_append` and `array_push_back`. To add an element to the start of a list, use the [`array_prepend` function](#athena-duckdb-functions-list-array-prepend) or the [`array_push_front` function](#athena-duckdb-functions-list-array-push-front).

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

```sql
array_append({{list}}, {{element}})
```

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

The `array_append` function takes the following arguments:

**{{list}}**  
The list to add to.

**{{element}}**  
The value to add. It must have the element type of `list`, or a type that DuckDB can convert to a common type with it.

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

A list of the element type of `list`, such as `INTEGER[]`. If `element` has a wider type, the result has the common type of both, for example a `DECIMAL` list when you add `2.5` to `[1, 2]`.

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

The following SQL query adds `4` to the end of a list.

```sql
SELECT array_append([2, 3], 4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3, 4]` | 

The following SQL query adds an element to a `NULL` list, which returns a list with one element, and adds a `NULL` element to a list.

```sql
SELECT
    array_append(
        CAST(NULL AS INTEGER[]), 4
    ) AS null_list,
    array_append([2, 3], NULL) AS null_element
```

The query returns the following result.


| null\_list | null\_element | 
| --- | --- | 
| `[4]` | `[2, 3, NULL]` | 

The following SQL query adds an element to an `INTEGER[2]` array and returns the type of the result, which is a list.

```sql
SELECT
    typeof(
        array_append([1, 2]::INTEGER[2], 3)
    ) AS result_type
```

The query returns the following result.


| result\_type | 
| --- | 
| `INTEGER[]` | 

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

If `element` cannot be converted to the element type of the list, as when you add a string to an `INTEGER` list, the query fails.

To add all the elements of another list, use the [`list_concat` function](duckdb-functions-list-creating-and-combining.md#athena-duckdb-functions-list-list-concat) or the `||` operator. If `element` is itself a list, `array_append` adds it as one element, which works only when `list` is a list of lists.

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

In Athena engine version 3, the `||` operator also adds a single element to the end of an array, as in `ARRAY[1, 2] || 3`. In DuckDB, `||` joins two lists only, so `[1, 2] || 3` fails. Use `array_append([1, 2], 3)`, or put the element in a list, as in `[1, 2] || [3]`.

## `array_pop_back` function
<a name="athena-duckdb-functions-list-array-pop-back"></a>

Returns `list` without its last element. For a list with one element, or for an empty list, the result is an empty list. If `list` is `NULL`, the result is `NULL`.

Unlike a pop operation in a programming language, the function returns the shortened list, not the element that it removes. To get the last element, use `{{list}}[-1]`. If you pass an `ARRAY` value, the result is a `LIST`.

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

```sql
array_pop_back({{list}})
```

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

The `array_pop_back` function takes the following argument:

**{{list}}**  
The list to remove the last element from.

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

A list of the element type of `list`, such as `INTEGER[]`.

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

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

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

The query returns the following result.


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

The following SQL query shows that a list with one element and an empty list both return an empty list.

```sql
SELECT
    array_pop_back([4]) AS one_element,
    array_pop_back(CAST([] AS INTEGER[])) AS empty_list
```

The query returns the following result.


| one\_element | empty\_list | 
| --- | --- | 
| `[]` | `[]` | 

The following SQL query returns the last element of a list and the other elements, in two columns.

```sql
SELECT
    [4, 5, 6][-1] AS last_element,
    array_pop_back([4, 5, 6]) AS other_elements
```

The query returns the following result.


| last\_element | other\_elements | 
| --- | --- | 
| `6` | `[4, 5]` | 

## `array_pop_front` function
<a name="athena-duckdb-functions-list-array-pop-front"></a>

Returns `list` without its first element. For a list with one element, or for an empty list, the result is an empty list. If `list` is `NULL`, the result is `NULL`.

Unlike a pop operation in a programming language, the function returns the shortened list, not the element that it removes. To get the first element, use `{{list}}[1]`. If you pass an `ARRAY` value, the result is a `LIST`.

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

```sql
array_pop_front({{list}})
```

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

The `array_pop_front` function takes the following argument:

**{{list}}**  
The list to remove the first element from.

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

A list of the element type of `list`, such as `INTEGER[]`.

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

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

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

The query returns the following result.


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

The following SQL query shows that a list with one element returns an empty list, and that a `NULL` list returns `NULL`.

```sql
SELECT
    array_pop_front([4]) AS one_element,
    array_pop_front(
        CAST(NULL AS INTEGER[])
    ) AS null_list
```

The query returns the following result.


| one\_element | null\_list | 
| --- | --- | 
| `[]` | `NULL` | 

The following SQL query returns the first element of a list and the other elements, in two columns.

```sql
SELECT
    [4, 5, 6][1] AS first_element,
    array_pop_front([4, 5, 6]) AS other_elements
```

The query returns the following result.


| first\_element | other\_elements | 
| --- | --- | 
| `4` | `[5, 6]` | 

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

Returns a new list that holds `element` followed by the elements of `list`. The element is added even when it is `NULL`. If `list` is `NULL`, the function treats it as an empty list and returns a list that holds only `element`. If you pass an `ARRAY` value, the result is a `LIST`.

The element comes first in the arguments, as it does in the result. The `array_push_front` function also adds an element to the start of a list, but it takes the list first. The `array_prepend` function returns the same result as `list_prepend`. To add an element to the end of a list, use the [`array_append` function](#athena-duckdb-functions-list-array-append).

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

```sql
array_prepend({{element}}, {{list}})
```

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

The `array_prepend` function takes the following arguments:

**{{element}}**  
The value to add. It must have the element type of `list`, or a type that DuckDB can convert to a common type with it.

**{{list}}**  
The list to add to.

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

A list of the element type of `list`, such as `INTEGER[]`. If `element` has a wider type, the result has the common type of both.

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

The following SQL query adds `1` to the start of a list.

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

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3]` | 

The following SQL query adds a `NULL` element to a list, and adds an element to a `NULL` list, which returns a list with one element.

```sql
SELECT
    array_prepend(NULL, [2, 3]) AS null_element,
    array_prepend(
        1, CAST(NULL AS INTEGER[])
    ) AS null_list
```

The query returns the following result.


| null\_element | null\_list | 
| --- | --- | 
| `[NULL, 2, 3]` | `[1]` | 

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

If you pass the list first, as in `array_prepend([2, 3], 1)`, the query fails. Put the element first, or use `array_push_front([2, 3], 1)`.

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

In Athena engine version 3, the `||` operator also adds a single element to the start of an array, as in `0 || ARRAY[1, 2]`. In DuckDB, `||` joins two lists only, so `0 || [1, 2]` fails. Use `array_prepend(0, [1, 2])`, or put the element in a list, as in `[0] || [1, 2]`.

## `array_push_back` function
<a name="athena-duckdb-functions-list-array-push-back"></a>

Returns a new list that holds the elements of `list` followed by `element`. The element is added even when it is `NULL`. If `list` is `NULL`, the function treats it as an empty list and returns a list that holds only `element`. If you pass an `ARRAY` value, the result is a `LIST`.

`array_push_back` returns the same result as `array_append` and `list_append`. To add an element to the start of a list, use the [`array_push_front` function](#athena-duckdb-functions-list-array-push-front).

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

```sql
array_push_back({{list}}, {{element}})
```

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

The `array_push_back` function takes the following arguments:

**{{list}}**  
The list to add to.

**{{element}}**  
The value to add. It must have the element type of `list`, or a type that DuckDB can convert to a common type with it.

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

A list of the element type of `list`, such as `INTEGER[]`. If `element` has a wider type, the result has the common type of both.

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

The following SQL query adds `4` to the end of a list.

```sql
SELECT array_push_back([2, 3], 4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3, 4]` | 

The following SQL query adds a `NULL` element to the end of a list of strings.

```sql
SELECT array_push_back(['a', 'b'], NULL) AS result
```

The query returns the following result.


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

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

If `element` cannot be converted to the element type of the list, as when you add a string to an `INTEGER` list, the query fails.

## `array_push_front` function
<a name="athena-duckdb-functions-list-array-push-front"></a>

Returns a new list that holds `element` followed by the elements of `list`. The element is added even when it is `NULL`. If `list` is `NULL`, the function treats it as an empty list and returns a list that holds only `element`. If you pass an `ARRAY` value, the result is a `LIST`.

`array_push_front` takes the list first and the element second. `array_prepend` returns the same result, with the arguments in the other order. To add an element to the end of a list, use the [`array_push_back` function](#athena-duckdb-functions-list-array-push-back).

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

```sql
array_push_front({{list}}, {{element}})
```

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

The `array_push_front` function takes the following arguments:

**{{list}}**  
The list to add to.

**{{element}}**  
The value to add. It must have the element type of `list`, or a type that DuckDB can convert to a common type with it.

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

A list of the element type of `list`, such as `INTEGER[]`. If `element` has a wider type, the result has the common type of both.

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

The following SQL query adds `1` to the start of a list.

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

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3]` | 

The following SQL query adds a `NULL` element to the start of a list, and adds an element to a `NULL` list, which returns a list with one element.

```sql
SELECT
    array_push_front(['b', 'c'], NULL) AS null_element,
    array_push_front(
        CAST(NULL AS VARCHAR[]), 'a'
    ) AS null_list
```

The query returns the following result.


| null\_element | null\_list | 
| --- | --- | 
| `[NULL, b, c]` | `[a]` | 

The following SQL query shows that `array_push_front` and `array_prepend` return the same list when you pass their arguments in opposite orders.

```sql
SELECT
    array_push_front([2, 3], 1) = array_prepend(1, [2, 3]) AS same_result
```

The query returns the following result.


| same\_result | 
| --- | 
| `true` | 

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

If you pass the element first, as in `array_push_front(1, [2, 3])`, the query fails. Put the list first, or use `array_prepend(1, [2, 3])`.

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

Returns `list` with `element` added as its last element. The type of `element` must match the element type of `list`, or both types must convert to a common type. A `NULL` element is added like any other value.

If `list` is `NULL`, it is treated as an empty list, so the result is a list with only `element`. To add an element at the start of a list, use the [`list_prepend` function](#athena-duckdb-functions-list-list-prepend). To join two lists, use the [`list_concat` function](duckdb-functions-list-creating-and-combining.md#athena-duckdb-functions-list-list-concat).

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

```sql
list_append({{list}}, {{element}})
```

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

The `list_append` function takes the following arguments:

**{{list}}**  
The list to add to, as a `LIST` value.

**{{element}}**  
The value to add, of the element type of `list`.

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

A `LIST` of the common type of the list elements and `element`, for example `INTEGER[]`.

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

The following SQL query adds `4` to the end of a list.

```sql
SELECT list_append([2, 3], 4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3, 4]` | 

The following SQL query adds an element to a `NULL` list, which is treated as empty, and adds a `NULL` element to a list.

```sql
SELECT
    list_append(NULL::INTEGER[], 4) AS null_list,
    list_append([2, 3], NULL) AS null_element
```

The query returns the following result.


| null\_list | null\_element | 
| --- | --- | 
| `[4]` | `[2, 3, NULL]` | 

The following SQL query adds a list as one element to a list of lists.

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

The query returns the following result.


| result | 
| --- | 
| `[[1, 2], [3, 4]]` | 

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

`list_append` adds one element. Passing a list as `element` to a list of single values, as in `list_append([1, 2], [3, 4])`, makes the query fail. To add the elements of a second list, use the [`list_concat` function](duckdb-functions-list-creating-and-combining.md#athena-duckdb-functions-list-list-concat).

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

In Athena engine version 3, `{{array}} || {{element}}` adds one element to the end of an array. In DuckDB, `||` joins two lists, and a list followed by a single value makes the query fail. Use `list_append({{list}}, {{element}})`, or wrap the element in a list, as in `{{list}} || [{{element}}]`.

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

Returns a new list that has `element` as its first element, followed by the elements of `list`. Pass the element first and the list second. This is the reverse of the argument order of `list_append`.

If `element` is `NULL`, the result starts with a `NULL` element. If `list` is `NULL`, the function treats it as an empty list, so the result is a list with one element.

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

```sql
list_prepend({{element}}, {{list}})
```

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

The `list_prepend` function takes the following arguments:

**{{element}}**  
The value to add, of a type that can be combined with the element type of `list`.

**{{list}}**  
The list to add the element to.

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

A list whose element type is the common type of `element` and the elements of `list`, for example `INTEGER[]`.

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

The following SQL query adds `1` to the start of a list.

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

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3]` | 

The following SQL query adds a `NULL` element to the start of a list.

```sql
SELECT list_prepend(NULL, [2, 3]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[NULL, 2, 3]` | 

The following SQL query passes a `NULL` list. The function treats it as an empty list and returns a list with one element.

```sql
SELECT
    list_prepend(1, CAST(NULL AS INTEGER[])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1]` | 

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

If you pass the list first and the element second, as in `list_prepend([2, 3], 1)`, the query fails. To add an element to the end of a list, use `list_append({{list}}, {{element}})`.

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

Returns a list with exactly `size` elements. If `list` has more than `size` elements, the function keeps the first `size` elements. If it has fewer, the function adds elements at the end, each set to `value`, or to `NULL` when you omit `value`.

If `list` is `NULL`, the result is `NULL`. If `size` is `NULL`, the result is an empty list. `array_resize` is an alias of `list_resize`.

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

```sql
list_resize({{list}}, {{size}} [, {{value}} ])
```

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

The `list_resize` function takes the following arguments:

**{{list}}**  
The list to resize.

**{{size}}**  
The number of elements in the result, as a non-negative integer.

**{{value}}**  
The value of each added element. It is converted to the element type of `list`. The default is `NULL`. This argument is optional.

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

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

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

The following SQL query extends a list to five elements and fills the new elements with `0`.

```sql
SELECT list_resize([1, 2, 3], 5, 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, 0, 0]` | 

The following SQL query uses the `array_resize` alias without `value`, so the new elements are `NULL`.

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

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, NULL, NULL]` | 

The following SQL query shortens a list to its first two elements.

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

The query returns the following result.


| result | 
| --- | 
| `[1, 2]` | 

The following SQL query passes `0.5` as `value` for a list of `INTEGER` values. The value is converted to `INTEGER`, which rounds it to `1`.

```sql
SELECT list_resize([1, 2, 3], 4, 0.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 2, 3, 1]` | 

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

A negative `size` makes the query fail.

Because `value` is converted to the element type of `list`, it can be rounded. For example, `list_resize([1, 2, 3], 4, 0.5)` returns `[1, 2, 3, 1]`, because `0.5` becomes the `INTEGER` value `1`. To keep the fraction, cast the list first, as in `list_resize(CAST([1, 2, 3] AS DOUBLE[]), 4, 0.5)`.