How to index a list in Gleam from the end?
10:40 19 Oct 2026

I want to split strings with various counts of - as separator. I need the fifth from the end and to discard results with fewer parts

let parts = string.split("1-22-333-4444-55555-6666-7777", on: "-")
let candidate = parts[-5] // <- This syntax does not work.

The documentation states:

Counting the length of a list or getting elements from other positions in the list is expensive and rarely done. It is rare to write algorithms that index into sequences in Gleam, but when they are written a list is not the right choice of data structure.

but it does not provide an alternative data structure. How can I access split off parts from the end?

How can I discard inputs if not enough parts were split off (like a case clause by list length)?

functional-programming gleam