You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform/website/docs/language/functions/element.mdx

52 lines
1.6 KiB

---
page_title: element - Functions - Configuration Language
description: The element function retrieves a single element from a list.
---
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
> [!IMPORTANT]
> **Documentation Update:** Product documentation previously located in `/website` has moved to the [`hashicorp/web-unified-docs`](https://github.com/hashicorp/web-unified-docs) repository, where all product documentation is now centralized. Please make contributions directly to `web-unified-docs`, since changes to `/website` in this repository will not appear on developer.hashicorp.com.
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
# `element` Function
`element` retrieves a single element from a list.
```hcl
element(list, index)
```
The index is zero-based. This function produces an error if used with an
empty list. The index can be a negative integer.
Use the built-in index syntax `list[index]` in most cases. Use this function
only for the special additional "wrap-around" behavior described below.
## Examples
```
> element(["a", "b", "c"], 1)
"b"
```
If the given index is greater than the length of the list then the index is
"wrapped around" by taking the index modulo the length of the list:
```
> element(["a", "b", "c"], 3)
"a"
```
To get the last element from the list use the index `-1`:
```
> element(["a", "b", "c"], -1)
"c"
```
## Related Functions
* [`index`](/terraform/language/functions/index_function) finds the index for a particular element value.
* [`lookup`](/terraform/language/functions/lookup) retrieves a value from a _map_ given its _key_.