---
page_title: slice function reference
description: The `slice` function extracts consecutive elements from a list. Learn how to use the `slice` function in Packer templates.
---

# `slice` Function

`slice` extracts some consecutive elements from within a list.

```hcl
slice(list, startindex, endindex)
```

`startindex` is inclusive, while `endindex` is exclusive. This function returns
an error if either index is outside the bounds of valid indices for the given
list.

## Examples

```shell-session
> slice(["a", "b", "c", "d"], 1, 3)
[
  "b",
  "c",
]
```

## Related Functions

- [`substr`](/packer/docs/templates/hcl_templates/functions/string/substr) performs a similar function for characters in a
  string, although it uses a length instead of an end index.
