Skip to content

Segment: Array Slices

William W. Kimball, Jr., MBA, MSIS edited this page Mar 1, 2020 · 4 revisions

YAML Path can return 0 or more elements from an array as a slice of its elements. The result is always an array, even if empty.

Starting with Segment: Array Elements notation, indicate both the start and end element indices as the index, separated via a single : symbol. Due to how Python calculates slices, please always remember that the start index is inclusive but the end element is exclusive; this is confusing. Negative indices can be used to identify elements from the end of the array rather than its beginning. For example:

- ignore0
- ignore1
- select2
- select3
- select4
- ignore5
- ignore6

To select elements 2 through 4, you can use any of these forms:

  • [2:5]
  • [2:-2]
  • [-5:-2]
  • [-5:5]

The [] pair is always required, even when using forward-slash notation.

Clone this wiki locally