github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/functions/element.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "element - Functions - Configuration Language"
     4  sidebar_current: "docs-funcs-collection-element"
     5  description: |-
     6    The element function retrieves a single element from a list.
     7  ---
     8  
     9  # `element` Function
    10  
    11  `element` retrieves a single element from a list.
    12  
    13  ```hcl
    14  element(list, index)
    15  ```
    16  
    17  The index is zero-based. This function produces an error if used with an
    18  empty list. The index must be a non-negative integer.
    19  
    20  Use the built-in index syntax `list[index]` in most cases. Use this function
    21  only for the special additional "wrap-around" behavior described below.
    22  
    23  ## Examples
    24  
    25  ```
    26  > element(["a", "b", "c"], 1)
    27  b
    28  ```
    29  
    30  If the given index is greater than the length of the list then the index is
    31  "wrapped around" by taking the index modulo the length of the list:
    32  
    33  ```
    34  > element(["a", "b", "c"], 3)
    35  a
    36  ```
    37  
    38  To get the last element from the list use [`length`](./length.html) to find
    39  the size of the list (minus 1 as the list is zero-based) and then pick the
    40  last element:
    41  
    42  ```
    43  > element(["a", "b", "c"], length(["a", "b", "c"])-1)
    44  c
    45  ```
    46  
    47  ## Related Functions
    48  
    49  * [`index`](./index_function.html) finds the index for a particular element value.
    50  * [`lookup`](./lookup.html) retrieves a value from a _map_ given its _key_.